@@ -41,12 +41,6 @@ static zend_always_inline bool php_json_check_stack_limit(void)
41
41
#endif
42
42
}
43
43
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
-
50
44
/* {{{ Pretty printing support functions */
51
45
52
46
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)
114
108
115
109
static zend_result php_json_encode_array (smart_str * buf , zval * val , int options , php_json_encoder * encoder ) /* {{{ */
116
110
{
117
- int r ;
111
+ bool encode_as_object = options & PHP_JSON_FORCE_OBJECT ;
118
112
bool need_comma = false;
119
113
HashTable * myht , * prop_ht ;
120
114
zend_refcounted * recursion_rc ;
@@ -131,7 +125,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
131
125
myht = Z_ARRVAL_P (val );
132
126
recursion_rc = (zend_refcounted * )myht ;
133
127
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 );
135
129
} else if (Z_OBJ_P (val )-> properties == NULL
136
130
&& Z_OBJ_HT_P (val )-> get_properties_for == NULL
137
131
&& 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,
219
213
* referenced from a different place in the object graph. */
220
214
recursion_rc = (zend_refcounted * )obj ;
221
215
}
222
- r = PHP_JSON_OUTPUT_OBJECT ;
216
+ encode_as_object = true ;
223
217
}
224
218
225
219
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,
231
225
232
226
PHP_JSON_HASH_PROTECT_RECURSION (recursion_rc );
233
227
234
- if (r == PHP_JSON_OUTPUT_ARRAY ) {
228
+ if (! encode_as_object ) {
235
229
smart_str_appendc (buf , '[' );
236
230
} else {
237
231
smart_str_appendc (buf , '{' );
@@ -250,7 +244,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
250
244
zval tmp ;
251
245
ZVAL_UNDEF (& tmp );
252
246
253
- if (r == PHP_JSON_OUTPUT_ARRAY ) {
247
+ if (! encode_as_object ) {
254
248
ZEND_ASSERT (Z_TYPE_P (data ) != IS_PTR );
255
249
256
250
if (need_comma ) {
@@ -262,7 +256,6 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
262
256
php_json_pretty_print_char (buf , options , '\n' );
263
257
php_json_pretty_print_indent (buf , options , encoder );
264
258
} else {
265
- ZEND_ASSERT (r == PHP_JSON_OUTPUT_OBJECT );
266
259
if (key ) {
267
260
if (ZSTR_VAL (key )[0 ] == '\0' && ZSTR_LEN (key ) > 0 && Z_TYPE_P (val ) == IS_OBJECT ) {
268
261
/* Skip protected and private members. */
@@ -346,7 +339,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
346
339
php_json_pretty_print_indent (buf , options , encoder );
347
340
}
348
341
349
- if (r == PHP_JSON_OUTPUT_ARRAY ) {
342
+ if (! encode_as_object ) {
350
343
smart_str_appendc (buf , ']' );
351
344
} else {
352
345
smart_str_appendc (buf , '}' );
0 commit comments