Skip to content

Commit 82a4f1a

Browse files
committed
Fix #66021 (Blank line inside empty array/object)
Changed json_encode() so that when the JSON_PRETTY_PRINT option is specified, the pair of linefeeds immediately after an opening bracket and before the corresponding closing bracket is omitted when the array or object contains no elements or accessible properties (and hence would have a blank line between the brackets).
1 parent d3fd163 commit 82a4f1a

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

ext/json/json.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static inline void json_pretty_print_indent(smart_str *buf, int options TSRMLS_D
219219

220220
static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) /* {{{ */
221221
{
222-
int i, r;
222+
int i, r, need_comma = 0;
223223
HashTable *myht;
224224

225225
if (Z_TYPE_PP(val) == IS_ARRAY) {
@@ -242,7 +242,6 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
242242
smart_str_appendc(buf, '{');
243243
}
244244

245-
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
246245
++JSON_G(encoder_depth);
247246

248247
i = myht ? zend_hash_num_elements(myht) : 0;
@@ -255,7 +254,6 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
255254
uint key_len;
256255
HashPosition pos;
257256
HashTable *tmp_ht;
258-
int need_comma = 0;
259257

260258
zend_hash_internal_pointer_reset_ex(myht, &pos);
261259
for (;; zend_hash_move_forward_ex(myht, &pos)) {
@@ -272,11 +270,11 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
272270
if (r == PHP_JSON_OUTPUT_ARRAY) {
273271
if (need_comma) {
274272
smart_str_appendc(buf, ',');
275-
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
276273
} else {
277274
need_comma = 1;
278275
}
279276

277+
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
280278
json_pretty_print_indent(buf, options TSRMLS_CC);
281279
php_json_encode(buf, *data, options TSRMLS_CC);
282280
} else if (r == PHP_JSON_OUTPUT_OBJECT) {
@@ -291,11 +289,11 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
291289

292290
if (need_comma) {
293291
smart_str_appendc(buf, ',');
294-
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
295292
} else {
296293
need_comma = 1;
297294
}
298295

296+
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
299297
json_pretty_print_indent(buf, options TSRMLS_CC);
300298

301299
json_escape_string(buf, key, key_len - 1, options & ~PHP_JSON_NUMERIC_CHECK TSRMLS_CC);
@@ -307,11 +305,11 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
307305
} else {
308306
if (need_comma) {
309307
smart_str_appendc(buf, ',');
310-
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
311308
} else {
312309
need_comma = 1;
313310
}
314311

312+
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
315313
json_pretty_print_indent(buf, options TSRMLS_CC);
316314

317315
smart_str_appendc(buf, '"');
@@ -333,8 +331,12 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
333331
}
334332

335333
--JSON_G(encoder_depth);
336-
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
337-
json_pretty_print_indent(buf, options TSRMLS_CC);
334+
335+
/* Only keep closing bracket on same line for empty arrays/objects */
336+
if (need_comma) {
337+
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
338+
json_pretty_print_indent(buf, options TSRMLS_CC);
339+
}
338340

339341
if (r == PHP_JSON_OUTPUT_ARRAY) {
340342
smart_str_appendc(buf, ']');

ext/json/tests/bug66021.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug #66021 (Blank line inside empty array/object when JSON_PRETTY_PRINT is set)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("json")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
class Foo {
9+
private $bar = 'baz';
10+
}
11+
12+
echo json_encode([[], (object)[], new Foo], JSON_PRETTY_PRINT), "\n";
13+
14+
?>
15+
--EXPECT--
16+
[
17+
[],
18+
{},
19+
{}
20+
]

0 commit comments

Comments
 (0)