Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions ext/json/json_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ static inline void php_json_encode_double(smart_str *buf, double d, int options)
static zend_result php_json_encode_array(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
{
bool encode_as_object = options & PHP_JSON_FORCE_OBJECT;
bool need_comma = false;
HashTable *myht, *prop_ht;
zend_refcounted *recursion_rc;

Expand Down Expand Up @@ -161,12 +160,6 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
continue;
}

if (need_comma) {
smart_str_appendc(buf, ',');
} else {
need_comma = true;
}

php_json_pretty_print_char(buf, options, '\n');
php_json_pretty_print_indent(buf, options, encoder);

Expand All @@ -186,6 +179,14 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
PHP_JSON_HASH_UNPROTECT_RECURSION(obj);
return FAILURE;
}

smart_str_appendc(buf, ',');
}

bool empty = ZSTR_VAL(buf->s)[ZSTR_LEN(buf->s) - 1] != ',';
if (!empty) {
/* Drop the trailing comma. */
ZSTR_LEN(buf->s)--;
}

PHP_JSON_HASH_UNPROTECT_RECURSION(obj);
Expand All @@ -197,7 +198,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
}
--encoder->depth;

if (need_comma) {
if (!empty) {
php_json_pretty_print_char(buf, options, '\n');
php_json_pretty_print_indent(buf, options, encoder);
}
Expand Down Expand Up @@ -235,6 +236,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,

uint32_t i = myht ? zend_hash_num_elements(myht) : 0;

bool empty = true;
if (i > 0) {
zend_string *key;
zval *data;
Expand All @@ -247,12 +249,6 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
if (!encode_as_object) {
ZEND_ASSERT(Z_TYPE_P(data) != IS_PTR);

if (need_comma) {
smart_str_appendc(buf, ',');
} else {
need_comma = true;
}

php_json_pretty_print_char(buf, options, '\n');
php_json_pretty_print_indent(buf, options, encoder);
} else {
Expand All @@ -276,11 +272,6 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
}
}

if (need_comma) {
smart_str_appendc(buf, ',');
} else {
need_comma = true;
}

php_json_pretty_print_char(buf, options, '\n');
php_json_pretty_print_indent(buf, options, encoder);
Expand All @@ -293,12 +284,6 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
smart_str_appendl(buf, "\"\"", 2);
}
} else {
if (need_comma) {
smart_str_appendc(buf, ',');
} else {
need_comma = true;
}

php_json_pretty_print_char(buf, options, '\n');
php_json_pretty_print_indent(buf, options, encoder);

Expand All @@ -319,7 +304,15 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
return FAILURE;
}
zval_ptr_dtor(&tmp);

smart_str_appendc(buf, ',');
} ZEND_HASH_FOREACH_END();

empty = ZSTR_VAL(buf->s)[ZSTR_LEN(buf->s) - 1] != ',';
if (!empty) {
/* Drop the trailing comma. */
ZSTR_LEN(buf->s)--;
}
}

PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc);
Expand All @@ -334,7 +327,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
--encoder->depth;

/* Only keep closing bracket on same line for empty arrays/objects */
if (need_comma) {
if (!empty) {
php_json_pretty_print_char(buf, options, '\n');
php_json_pretty_print_indent(buf, options, encoder);
}
Expand Down
Loading