php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #42001 json_encode() does not work on all types
Submitted: 2007-07-15 12:49 UTC Modified: 2007-08-16 12:14 UTC
From: stefan dot priebsch at e-novative dot de Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: irrelevant
Private report: No CVE-ID:
 [2007-07-15 12:49 UTC] stefan dot priebsch at e-novative dot de
Description:
------------
The manual entry for json_encode() says the function works on any type except a resource. This is not accurate. json_encode() does not work properly on SimpleXML data that contains CDATA (see bug#41976).

Documentation should be changed to reflect this.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-16 09:06 UTC] tony2001@php.net
Please read the explanation in the original report, there is nothing wrong in the docs.
 [2007-07-16 21:43 UTC] stefan dot priebsch at e-novative dot de
json_encode() does not work with SimpleXML data that contains CDATA. Either this is a bug in PHP (which you claim it is not, bogusifying my bug report) or the documentation is inaccurate.

As a PHP developer I don't really care wether SimpleXML data is serialized to json_encode() it or not, I care wether the conversion works for all types as the manual suggests - and it does not.

As you don't consider #41976 a bug in PHP, you should at least change the manual. You can't just claim json_encode() works on any data type, when it does not.
 [2007-07-16 21:52 UTC] tony2001@php.net
>json_encode() does not work with SimpleXML data that contains CDATA.

json_encode() encodes THE OBJECT, but you actually meant to encode IT'S DATA. To do that you need to cast it to string, as I've said in the original report.

>Either this is a bug in PHP (which you claim it is not, bogusifying
>my bug report) or the documentation is inaccurate.

The function works as expected and the docs are correct.
Please read what I'm writing to you.
We're not going to explain every possible user error in the docs, it would take gigabytes otherwise.
 [2007-07-17 07:46 UTC] stefan dot priebsch at e-novative dot de
Anthony, I am terribly sorry, but I completely fail to see your point. What is the difference between an object and its data? An object is code+data, and I am sure that we'll agree that no code will ever be serialized or show up when something is cast to string.

Besides, look at more complex examples: They work fine when *not* cast to string. When casting to string, json_encode() does not work at all. When json_encoding a complex XML structure, however, CDATA still does not show up - this is a bug.

Example:

  $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><test><sub>test</sub><another>value</another><path><to><tag>something</tag></to></path></test>');
  var_dump(json_encode($xml));
  var_dump(json_encode((string) $xml));

  $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><test><sub><![CDATA[test]]></sub><another>value</another><path><to><tag>something</tag></to></path></test>');
  var_dump(json_encode($xml));
  var_dump(json_encode((string) $xml));

So the situation is: you are telling me to cast to string before encoding. This works on "primitive" XML, but not on real one with nested tags. Encoding nested tags works fine if not casting to string, though, except for the fact that CDATA does not show up.
 [2007-07-17 07:54 UTC] derick@php.net
Well, although I also think this could be a bug, it should at least be clarified in the documentation as it's not clear enough now it seems.

(Please don't change the summary though)
 [2007-08-16 12:14 UTC] vrana@php.net
Problem is that SimpleXML ignored CDATA. json_encode() thus cannot encode it.
 [2013-07-09 22:47 UTC] hanskrentel at yahoo dot de
for json_encode() objects are first cast into an array. then they are 
JSON encoded as JSON objects.

The problems discussed here are therefore actually caused by the weird 
way how simplexmlelement has implemented the to-array cast.

Next to empty or whitespace-only prefixed CDATA nodes more problems can 
be seen with all DOM node-types simplexmlelement does not give an 
interface for: comments and processing instructions.

normally those are not accessible, however when casting to array those 
are offered in a somehow crippled way.

just leaving a comment here as i haven't seen much other related 
discussion about the topic online.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 07:02:02 2014 UTC