Doc #79343 [Asn->Csd]: json_encode fails "silently" for DOMDocument

From: Date: Sat, 07 Mar 2020 22:00:18 +0000
Subject: Doc #79343 [Asn->Csd]: json_encode fails "silently" for DOMDocument
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17382@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: phpdocbot@php.net Reported by: fred5 at originsystems dot co dot za Summary: json_encode fails "silently" for DOMDocument -Status: Assigned +Status: Closed Type: Documentation Problem Package: JSON related Operating System: Centos 7 PHP Version: 7.4.3 Assigned To: googleguy Block user comment: N Private report: N New Comment: Automatic comment on behalf of googleguy Revision: http://git.php.net/?p=doc/en.git;a=commit;h=fd130ca78e6d467f668a36349ba1f1fb7c6396b5 Log: Fix bug #79343 Previous Comments: ------------------------------------------------------------------------ [2020-03-07 19:20:18] googleguy@php.net Automatic comment from SVN on behalf of googleguy Revision: http://svn.php.net/viewvc/?view=revision&revision=349342 Log: Fix bug #79343 json_encode fails "silently" for DOMDocument. Updated documentation to reflect that calling json_encode() on a DOMDocument object will result in any empty object being encoded. ------------------------------------------------------------------------ [2020-03-04 15:33:20] googleguy@php.net I will add this to the documentation. ------------------------------------------------------------------------ [2020-03-04 15:31:48] googleguy@php.net Believe it or not, this is actually expected behavior in DOMDOcument. DOMDocument has no sensible public property representation in PHP user space. So since json_encode only encodes publicly accessible properties the result is an empty object. There is no reason to expect an error here because one did not occur. ------------------------------------------------------------------------ [2020-03-04 13:42:21] cmb@php.net > 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 ------------------------------------------------------------------------ [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. ... ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=79343 -- Edit this bug report at https://bugs.php.net/bug.php?id=79343&edit=1

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