Bug #79343 [Opn]: json_encode fails "silently" for DOMDocument
| From: | fred5 at originsystems dot co dot za | Date: | Wed, 04 Mar 2020 13:16:12 +0000 |
| Subject: | Bug #79343 [Opn]: json_encode fails "silently" for DOMDocument | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225883@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79343&edit=1
ID: 79343
User updated by: fred5 at originsystems dot co dot za
Reported by: fred5 at originsystems dot co dot za
Summary: json_encode fails "silently" for DOMDocument
Status: Open
Type: Bug
Package: *General Issues
Operating System: Centos 7
PHP Version: 7.4.3
Block user comment: N
Private report: N
New Comment:
Apologies, the description should read
...
Instead just returns {}, entirely ignoring the contents of the DOMDocument it is meant to be
ENcoding.
...
Previous Comments:
------------------------------------------------------------------------
[2020-03-04 13:14:50] fred5 at originsystems dot co dot za
Description:
------------
I have tried to json_encode a DOMDocument and does not return false and does not throw exception.
Instead just returns {}, entirely ignoring the contents of the DOMDocument it is meant to be
decoding.
The problem for me here is that the json_encode logically "fails" but does not indicate
this.
(In an ideal world I would expect it to decode the DOMDocument in exactly the same way as it does a
SimpleXML document
Test script:
---------------
<?php
$a = new DOMDocument();
$a->appendChild($a->createElement("hello","there"));
print "Encode DOMDocument" . PHP_EOL;
if (($output = json_encode($a)) === false) {
print "fails with error!";
} else {
print $output;
}
print PHP_EOL . PHP_EOL;
print "Encode DOMDocument document element" . PHP_EOL;
if (($output = json_encode($a->documentElement)) === false) {
print "fails with error!";
} else {
print $output;
}
print PHP_EOL . PHP_EOL;
print "Encode SimpleXML equivalent" . PHP_EOL;
if (($output = json_encode(simplexml_import_dom($a->documentElement))) === false) {
print "fails with error!";
} else {
print $output;
}
print PHP_EOL . PHP_EOL;
Expected result:
----------------
I would expect either DOMDocument or DOMDocument document element to encode sensibly.
Or at the very least I would expect json_encode to return false or throw exception should the
failure be considered catastrophic.
Actual result:
--------------
Encode DOMDocument
{}
Encode DOMDocument document element
{}
Encode SimpleXML equivalent
{"0":"there"}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79343&edit=1