com php-src: Fix #66021 (Blank line inside empty array/object): ext/json/json.c ext/json/tests/bug66021.phpt

From: Date: Sat, 02 Nov 2013 19:59:11 +0000
Subject: com php-src: Fix #66021 (Blank line inside empty array/object): ext/json/json.c ext/json/tests/bug66021.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-76865@lists.php.net to get a copy of this message
Commit: 82a4f1a1a287d9dbf01156bc14ceb13ccbf16d7a Author: Kevin Israel <pleasestand@live.com> Sat, 2 Nov 2013 15:47:24 -0400 Parents: d3fd163d2734a3b615eb76ca593b79590b3fd4ae Branches: PHP-5.4 PHP-5.5 PHP-5.6 master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=82a4f1a1a287d9dbf01156bc14ceb13ccbf16d7a Log: 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). Bugs: https://bugs.php.net/66021 Changed paths: M ext/json/json.c A ext/json/tests/bug66021.phpt Diff: diff --git a/ext/json/json.c b/ext/json/json.c index 8c8963d..46f61d7 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -219,7 +219,7 @@ static inline void json_pretty_print_indent(smart_str *buf, int options TSRMLS_D static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) /* {{{ */ { - int i, r; + int i, r, need_comma = 0; HashTable *myht; 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) smart_str_appendc(buf, '{'); } - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); ++JSON_G(encoder_depth); 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) uint key_len; HashPosition pos; HashTable *tmp_ht; - int need_comma = 0; zend_hash_internal_pointer_reset_ex(myht, &pos); 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) if (r == PHP_JSON_OUTPUT_ARRAY) { if (need_comma) { smart_str_appendc(buf, ','); - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); } else { need_comma = 1; } + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); json_pretty_print_indent(buf, options TSRMLS_CC); php_json_encode(buf, *data, options TSRMLS_CC); } 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) if (need_comma) { smart_str_appendc(buf, ','); - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); } else { need_comma = 1; } + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); json_pretty_print_indent(buf, options TSRMLS_CC); 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) } else { if (need_comma) { smart_str_appendc(buf, ','); - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); } else { need_comma = 1; } + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); json_pretty_print_indent(buf, options TSRMLS_CC); smart_str_appendc(buf, '"'); @@ -333,8 +331,12 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) } --JSON_G(encoder_depth); - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); - json_pretty_print_indent(buf, options TSRMLS_CC); + + /* Only keep closing bracket on same line for empty arrays/objects */ + if (need_comma) { + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); + json_pretty_print_indent(buf, options TSRMLS_CC); + } if (r == PHP_JSON_OUTPUT_ARRAY) { smart_str_appendc(buf, ']'); diff --git a/ext/json/tests/bug66021.phpt b/ext/json/tests/bug66021.phpt new file mode 100644 index 0000000..cf52e0c --- /dev/null +++ b/ext/json/tests/bug66021.phpt @@ -0,0 +1,20 @@ +--TEST-- +Bug #66021 (Blank line inside empty array/object when JSON_PRETTY_PRINT is set) +--SKIPIF-- +<?php if (!extension_loaded("json")) print "skip"; ?> +--FILE-- +<?php + +class Foo { + private $bar = 'baz'; +} + +echo json_encode([[], (object)[], new Foo], JSON_PRETTY_PRINT), "\n"; + +?> +--EXPECT-- +[ + [], + {}, + {} +]

« previous php.cvs (#76865) next »