Skip to content

Commit 92aeda5

Browse files
committed
Fix json_encode regression with JSON_PRETTY_PRINT
This makes the json encoding behavior the same as it was prior to the memory optimizations added in f9f8c1c (for objects with declared properties) This is based on the code for the unoptimized case below the changes. Buggy output prior to this commit: ``` { "prop":"value"} ``` Correct output: ``` { "prop": "value" } ``` Closes phpGH-6811
1 parent e0e3d98 commit 92aeda5

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

ext/json/json_encoder.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static int php_json_encode_array(smart_str *buf, zval *val, int options, php_jso
119119
} else if (Z_OBJ_P(val)->properties == NULL
120120
&& Z_OBJ_HT_P(val)->get_properties_for == NULL
121121
&& Z_OBJ_HT_P(val)->get_properties == zend_std_get_properties) {
122-
/* Optimized version without rebulding properties HashTable */
122+
/* Optimized version without rebuilding properties HashTable */
123123
zend_object *obj = Z_OBJ_P(val);
124124
zend_class_entry *ce = obj->ce;
125125
zend_property_info *prop_info;
@@ -133,7 +133,11 @@ static int php_json_encode_array(smart_str *buf, zval *val, int options, php_jso
133133
}
134134

135135
PHP_JSON_HASH_PROTECT_RECURSION(obj);
136+
136137
smart_str_appendc(buf, '{');
138+
139+
++encoder->depth;
140+
137141
for (i = 0; i < ce->default_properties_count; i++) {
138142
prop_info = ce->properties_info_table[i];
139143
if (!prop_info) {
@@ -174,6 +178,13 @@ static int php_json_encode_array(smart_str *buf, zval *val, int options, php_jso
174178
return FAILURE;
175179
}
176180
}
181+
182+
--encoder->depth;
183+
184+
if (need_comma) {
185+
php_json_pretty_print_char(buf, options, '\n');
186+
php_json_pretty_print_indent(buf, options, encoder);
187+
}
177188
smart_str_appendc(buf, '}');
178189
PHP_JSON_HASH_UNPROTECT_RECURSION(obj);
179190
return SUCCESS;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
json_encode() with JSON_PRETTY_PRINT on declared properties
3+
--FILE--
4+
<?php
5+
class MyClass {
6+
public $x;
7+
public $y;
8+
public function __construct($x = 123, $y = []) {
9+
$this->x = $x;
10+
$this->y = $y;
11+
}
12+
}
13+
14+
class HasNoProperties {}
15+
16+
echo json_encode(new HasNoProperties()), "\n";
17+
echo json_encode(new HasNoProperties(), JSON_PRETTY_PRINT), "\n";
18+
19+
echo json_encode(new MyClass()), "\n";
20+
echo json_encode(new MyClass(), JSON_PRETTY_PRINT), "\n";
21+
$obj = new MyClass();
22+
$obj->dynamic = new MyClass(null, []);
23+
echo json_encode($obj), "\n";
24+
echo json_encode($obj, JSON_PRETTY_PRINT), "\n";
25+
$obj = new MyClass();
26+
unset($obj->y);
27+
echo json_encode($obj), "\n";
28+
echo json_encode($obj, JSON_PRETTY_PRINT), "\n";
29+
unset($obj->x);
30+
echo json_encode($obj), "\n";
31+
echo json_encode($obj, JSON_PRETTY_PRINT), "\n";
32+
?>
33+
--EXPECT--
34+
{}
35+
{}
36+
{"x":123,"y":[]}
37+
{
38+
"x": 123,
39+
"y": []
40+
}
41+
{"x":123,"y":[],"dynamic":{"x":null,"y":[]}}
42+
{
43+
"x": 123,
44+
"y": [],
45+
"dynamic": {
46+
"x": null,
47+
"y": []
48+
}
49+
}
50+
{"x":123}
51+
{
52+
"x": 123
53+
}
54+
{}
55+
{}

0 commit comments

Comments
 (0)