Skip to content

Commit

Permalink
Fix json_encode result on DOMDocument
Browse files Browse the repository at this point in the history
According to https://www.php.net/manual/en/class.domdocument:
  When using json_encode() on a DOMDocument object the result will be
  that of encoding an empty object.

But this was broken in 8.1. The output was `{"config": null}`.
That's because the config property is defined with a default value of
NULL, hence it was included. The other properties are not included
because they don't have a default property, and nothing is ever written
to their backing field. Hence, the JSON encoder excludes them.
Similarly, `(array) $doc` would yield the same `config` key in the
array.

Closes phpGH-11840.
  • Loading branch information
nielsdos authored and bon committed Aug 16, 2023
1 parent e3a62f8 commit c483112
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PHP NEWS
. Fix empty argument cases for DOMParentNode methods. (nielsdos)
. Fixed bug GH-11791 (Wrong default value of DOMDocument::xmlStandalone).
(nielsdos)
. Fix json_encode result on DOMDocument. (nielsdos)

- Core:
. Fixed oss-fuzz #60741 (Leak in open_basedir). (ilutov)
Expand Down
2 changes: 1 addition & 1 deletion ext/dom/php_dom.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ class DOMDocument extends DOMNode implements DOMParentNode
* @readonly
* @deprecated
*/
public mixed $config = null;
public mixed $config;

public bool $formatOutput;

Expand Down
11 changes: 11 additions & 0 deletions ext/dom/tests/DOMDocument_json_encode.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
JSON encoding a DOMDocument
--EXTENSIONS--
dom
--FILE--
<?php
$doc = new DOMDocument;
echo json_encode($doc);
?>
--EXPECT--
{}

0 comments on commit c483112

Please sign in to comment.