summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStanislav Malyshev <stas@php.net>2014-04-13 18:50:39 -0700
committerStanislav Malyshev <stas@php.net>2014-04-13 18:50:39 -0700
commit5e66ce929350b98003becd362464d47e8f49c8fb (patch)
tree69da0092e11c13e3b5677845d6acf1ce06650547
parentb05c088a3abf8e4c6fb6e40418423a9e2dd3d929 (diff)
parent82a4f1a1a287d9dbf01156bc14ceb13ccbf16d7a (diff)
downloadphp-git-5e66ce929350b98003becd362464d47e8f49c8fb.tar.gz
Merge branch 'pull-request/518' into PHP-5.4
* pull-request/518: Fix #66021 (Blank line inside empty array/object)
-rw-r--r--ext/json/json.c18
-rw-r--r--ext/json/tests/bug66021.phpt20
2 files changed, 30 insertions, 8 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index 37ee314936..b3240e1b4b 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 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--
+[
+ [],
+ {},
+ {}
+]