Skip to content

Commit c483112

Browse files
nielsdosbon
authored and
bon
committed
Fix json_encode result on DOMDocument
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.
1 parent e3a62f8 commit c483112

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ PHP NEWS
1313
. Fix empty argument cases for DOMParentNode methods. (nielsdos)
1414
. Fixed bug GH-11791 (Wrong default value of DOMDocument::xmlStandalone).
1515
(nielsdos)
16+
. Fix json_encode result on DOMDocument. (nielsdos)
1617

1718
- Core:
1819
. Fixed oss-fuzz #60741 (Leak in open_basedir). (ilutov)

ext/dom/php_dom.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ class DOMDocument extends DOMNode implements DOMParentNode
707707
* @readonly
708708
* @deprecated
709709
*/
710-
public mixed $config = null;
710+
public mixed $config;
711711

712712
public bool $formatOutput;
713713

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
JSON encoding a DOMDocument
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$doc = new DOMDocument;
8+
echo json_encode($doc);
9+
?>
10+
--EXPECT--
11+
{}

0 commit comments

Comments
 (0)