diff options
author | Stanislav Malyshev <stas@php.net> | 2014-04-13 18:54:54 -0700 |
---|---|---|
committer | Stanislav Malyshev <stas@php.net> | 2014-04-13 18:55:28 -0700 |
commit | 9bba219c75b0fa82846bc2fa260888cdc97ac6e6 (patch) | |
tree | f8416289eda8a7a157e21b7fd68c4dc47da7b71e | |
parent | 30ec1c005188e523eabc90741b3d8eda0cb81070 (diff) | |
parent | 5e66ce929350b98003becd362464d47e8f49c8fb (diff) | |
download | php-git-9bba219c75b0fa82846bc2fa260888cdc97ac6e6.tar.gz |
Merge branch 'PHP-5.4' into PHP-5.5
* PHP-5.4:
Fix #66021 (Blank line inside empty array/object)
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | ext/json/json.c | 18 | ||||
-rw-r--r-- | ext/json/tests/bug66021.phpt | 20 |
3 files changed, 34 insertions, 8 deletions
@@ -31,6 +31,10 @@ PHP NEWS - FPM: . Fixed bug #66482 (unknown entry 'priority' in php-fpm.conf). +- JSON: + . Fixed bug #66021 (Blank line inside empty array/object when + JSON_PRETTY_PRINT is set). (Kevin Israel) + - mysqli: . Fixed problem in mysqli_commit()/mysqli_rollback() with second parameter (extra comma) and third parameters (lack of escaping). (Andrey) diff --git a/ext/json/json.c b/ext/json/json.c index b0191c38f5..6b66dce3df 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -230,7 +230,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) { @@ -253,7 +253,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; @@ -266,7 +265,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)) { @@ -283,11 +281,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) { @@ -302,11 +300,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); @@ -318,11 +316,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, '"'); @@ -347,8 +345,12 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) JSON_G(error_code) = PHP_JSON_ERROR_DEPTH; } --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 0000000000..cf52e0c6df --- /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-- +[ + [], + {}, + {} +] |