#41976 [Opn->Bgs]: json_encode ignores CDATA sections

From: Date: Fri, 13 Jul 2007 08:08:03 +0000
Subject: #41976 [Opn->Bgs]: json_encode ignores CDATA sections
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-115018@lists.php.net to get a copy of this message
 ID:               41976
 Updated by:       johannes@php.net
 Reported By:      stefan dot priebsch at e-novative dot de
-Status:           Open
+Status:           Bogus
 Bug Type:         JSON related
 Operating System: WinXP
 PHP Version:      5.2.3
 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

Yes, and it will try to serialize the SimpleXML object, what won't
work, that's part of SimpleXML's behaviour 


Previous Comments:
------------------------------------------------------------------------

[2007-07-13 06:33:16] stefan dot priebsch at e-novative dot de

Sorry, I disagree. This is a bug, not bogus.

The manual says that json_encode works on any type except resources. 

  $xml = new SimpleXMLElement('<?xml version="1.0"
encoding="UTF-8"?><test><![CDATA[test]]></test>');
  var_dump(is_resource($xml));

outputs false, (which is obvious as SimpleXML is not listed in the list
of resource types). Thus SimpleXML is not a resource and must be
processed by json_encode() without a string cast. How would I supposed
to cast an XML tree to string anyway?

Please have a look at bug#38680, where a similar issue was discussed.

------------------------------------------------------------------------

[2007-07-12 09:08:39] tony2001@php.net

  var_dump((string) $xml);
  var_dump(json_encode($xml)); <-- you have to cast it to string
explicitly, otherwise json_encode() will encode the object itself, not
it's string representation.

------------------------------------------------------------------------

[2007-07-12 07:39:30] stefan dot priebsch at e-novative dot de

Description:
------------
When json_encoding (Simple)XML data, CDATA sections are ignored.

Reproduce code:
---------------
  $xml = new SimpleXMLElement('<?xml version="1.0"
encoding="UTF-8"?><test>test</test>');
  var_dump((string) $xml);
  var_dump(json_encode($xml));

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


Expected result:
----------------
string 'test' (length=4)

string '{"0":"test"}' (length=12)

string 'test' (length=4)

string '{"0":"test"}' (length=12)


Actual result:
--------------
string 'test' (length=4)

string '{"0":"test"}' (length=12)

string 'test' (length=4)

string '{}' (length=2)



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=41976&edit=1


Thread (10 messages)

« previous php.bugs (#115018) next »