Bug #79343 [NEW]: json_encode fails "silently" for DOMDocument

From: Date: Wed, 04 Mar 2020 13:14:50 +0000
Subject: Bug #79343 [NEW]: json_encode fails "silently" for DOMDocument
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225882@lists.php.net to get a copy of this message
From: fred5 at originsystems dot co dot za Operating system: Centos 7 PHP version: 7.4.3 Package: *General Issues Bug Type: Bug Bug description:json_encode fails "silently" for DOMDocument 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 bug report at https://bugs.php.net/bug.php?id=79343&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=79343&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=79343&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=79343&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=79343&r=needscript Try newer version: https://bugs.php.net/fix.php?id=79343&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=79343&r=support Expected behavior: https://bugs.php.net/fix.php?id=79343&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=79343&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=79343&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=79343&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=79343&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=79343&r=dst IIS Stability: https://bugs.php.net/fix.php?id=79343&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=79343&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=79343&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=79343&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=79343&r=mysqlcfg

« previous php.bugs (#225882) next »