php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #79343 json_encode fails "silently" for DOMDocument
Submitted: 2020-03-04 13:14 UTC Modified: 2020-03-04 15:33 UTC
From: fred5 at originsystems dot co dot za Assigned: googleguy (profile)
Status: Closed Package: JSON related
PHP Version: 7.4.3 OS: Centos 7
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: fred5 at originsystems dot co dot za
New email:
PHP Version: OS:

 

 [2020-03-04 13:14 UTC] 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"}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-04 13:16 UTC] 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:42 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem -Package: *General Issues +Package: JSON related
 [2020-03-04 13:42 UTC] 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 15:31 UTC] 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 15:33 UTC] googleguy@php.net
-Assigned To: +Assigned To: googleguy
 [2020-03-04 15:33 UTC] googleguy@php.net
I will add this to the documentation.
 [2020-03-07 19:20 UTC] googleguy@php.net
Automatic comment from SVN on behalf of googleguy
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=349342
Log: Fix bug #79343

json_encode fails &quot;silently&quot; for DOMDocument.

Updated documentation to reflect that calling json_encode() on a DOMDocument object
will result in any empty object being encoded.
 [2020-03-07 22:00 UTC] phpdocbot@php.net
Automatic comment on behalf of googleguy
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=fd130ca78e6d467f668a36349ba1f1fb7c6396b5
Log: Fix bug #79343
 [2020-03-07 22:00 UTC] phpdocbot@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC