Skip to content

Commit cbeb323

Browse files
committed
ext/json: Refactor php_json_encode_array()
Stop relying on internal flags which were published in the header Use a more descriptive variable name and have it be a boolean
1 parent d8e13ee commit cbeb323

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

ext/json/json_encoder.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ static zend_always_inline bool php_json_check_stack_limit(void)
4141
#endif
4242
}
4343

44-
static int php_json_determine_array_type(const HashTable *ht) /* {{{ */
45-
{
46-
return zend_array_is_list(ht) ? PHP_JSON_OUTPUT_ARRAY : PHP_JSON_OUTPUT_OBJECT;
47-
}
48-
/* }}} */
49-
5044
/* {{{ Pretty printing support functions */
5145

5246
static inline void php_json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */
@@ -114,7 +108,7 @@ static inline void php_json_encode_double(smart_str *buf, double d, int options)
114108

115109
static zend_result php_json_encode_array(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
116110
{
117-
int r;
111+
bool encode_as_object = options & PHP_JSON_FORCE_OBJECT;
118112
bool need_comma = false;
119113
HashTable *myht, *prop_ht;
120114
zend_refcounted *recursion_rc;
@@ -131,7 +125,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
131125
myht = Z_ARRVAL_P(val);
132126
recursion_rc = (zend_refcounted *)myht;
133127
prop_ht = NULL;
134-
r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : php_json_determine_array_type(myht);
128+
encode_as_object = encode_as_object || !zend_array_is_list(myht);
135129
} else if (Z_OBJ_P(val)->properties == NULL
136130
&& Z_OBJ_HT_P(val)->get_properties_for == NULL
137131
&& Z_OBJ_HT_P(val)->get_properties == zend_std_get_properties
@@ -219,7 +213,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
219213
* referenced from a different place in the object graph. */
220214
recursion_rc = (zend_refcounted *)obj;
221215
}
222-
r = PHP_JSON_OUTPUT_OBJECT;
216+
encode_as_object = true;
223217
}
224218

225219
if (recursion_rc && GC_IS_RECURSIVE(recursion_rc)) {
@@ -231,7 +225,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
231225

232226
PHP_JSON_HASH_PROTECT_RECURSION(recursion_rc);
233227

234-
if (r == PHP_JSON_OUTPUT_ARRAY) {
228+
if (!encode_as_object) {
235229
smart_str_appendc(buf, '[');
236230
} else {
237231
smart_str_appendc(buf, '{');
@@ -250,7 +244,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
250244
zval tmp;
251245
ZVAL_UNDEF(&tmp);
252246

253-
if (r == PHP_JSON_OUTPUT_ARRAY) {
247+
if (!encode_as_object) {
254248
ZEND_ASSERT(Z_TYPE_P(data) != IS_PTR);
255249

256250
if (need_comma) {
@@ -262,7 +256,6 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
262256
php_json_pretty_print_char(buf, options, '\n');
263257
php_json_pretty_print_indent(buf, options, encoder);
264258
} else {
265-
ZEND_ASSERT(r == PHP_JSON_OUTPUT_OBJECT);
266259
if (key) {
267260
if (ZSTR_VAL(key)[0] == '\0' && ZSTR_LEN(key) > 0 && Z_TYPE_P(val) == IS_OBJECT) {
268261
/* Skip protected and private members. */
@@ -346,7 +339,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
346339
php_json_pretty_print_indent(buf, options, encoder);
347340
}
348341

349-
if (r == PHP_JSON_OUTPUT_ARRAY) {
342+
if (!encode_as_object) {
350343
smart_str_appendc(buf, ']');
351344
} else {
352345
smart_str_appendc(buf, '}');

ext/json/php_json.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ typedef enum {
7979
#define PHP_JSON_INVALID_UTF8_SUBSTITUTE (1<<21)
8080
#define PHP_JSON_THROW_ON_ERROR (1<<22)
8181

82-
/* Internal flags */
83-
#define PHP_JSON_OUTPUT_ARRAY 0
84-
#define PHP_JSON_OUTPUT_OBJECT 1
85-
8682
/* default depth */
8783
#define PHP_JSON_PARSER_DEFAULT_DEPTH 512
8884

0 commit comments

Comments
 (0)