@@ -219,7 +219,7 @@ static inline void json_pretty_print_indent(smart_str *buf, int options TSRMLS_D
219219
220220static 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 , ']' );
0 commit comments