Bug->Doc #79343 [Opn]: json_encode fails "silently" for DOMDocument

From: Date: Wed, 04 Mar 2020 13:42:21 +0000
Subject: Bug->Doc #79343 [Opn]: json_encode fails "silently" for DOMDocument
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17361@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 Updated by: cmb@php.net Reported by: fred5 at originsystems dot co dot za Summary: json_encode fails "silently" for DOMDocument Status: Open -Type: Bug +Type: Documentation Problem -Package: *General Issues +Package: JSON related Operating System: Centos 7 PHP Version: 7.4.3 Block user comment: N Private report: N New Comment: > The problem for me here is that the json_encode logically > "fails" but does not indicate this. No, it does not fail. If a class does not implement JsonSerializable, its instances are json_encode()d as if the object had been cast to array; however, casting any DOMDocument to array yields an empty array. This should be documented. > In an ideal world I would expect it to decode the DOMDocument in > exactly the same way as it does a SimpleXML document You could simplexml_import_dom() the DOMDocument, and json_encode() the result Previous Comments: ------------------------------------------------------------------------ [2020-03-04 13:16:12] fred5 at originsystems dot co dot za Apologies, the description should read ... Instead just returns {}, entirely ignoring the contents of the DOMDocument it is meant to be ENcoding. ... ------------------------------------------------------------------------ [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

« previous php.doc.bugs (#17361) next »