mirror of
https://github.com/php/php-src.git
synced 2025-01-23 20:23:31 +08:00
MFH Add PHP_JSON_FORCE_OBJECT for forcing an object output of an array
This commit is contained in:
parent
b117752f8b
commit
8d2a5cabec
@ -41,6 +41,7 @@ static const char digits[] = "0123456789abcdef";
|
||||
#define PHP_JSON_HEX_AMP (1<<1)
|
||||
#define PHP_JSON_HEX_APOS (1<<2)
|
||||
#define PHP_JSON_HEX_QUOT (1<<3)
|
||||
#define PHP_JSON_FORCE_OBJECT (1<<4)
|
||||
|
||||
ZEND_DECLARE_MODULE_GLOBALS(json)
|
||||
|
||||
@ -75,6 +76,7 @@ static PHP_MINIT_FUNCTION(json)
|
||||
REGISTER_LONG_CONSTANT("JSON_HEX_AMP", PHP_JSON_HEX_AMP, CONST_CS | CONST_PERSISTENT);
|
||||
REGISTER_LONG_CONSTANT("JSON_HEX_APOS", PHP_JSON_HEX_APOS, CONST_CS | CONST_PERSISTENT);
|
||||
REGISTER_LONG_CONSTANT("JSON_HEX_QUOT", PHP_JSON_HEX_QUOT, CONST_CS | CONST_PERSISTENT);
|
||||
REGISTER_LONG_CONSTANT("JSON_FORCE_OBJECT", PHP_JSON_FORCE_OBJECT, CONST_CS | CONST_PERSISTENT);
|
||||
|
||||
REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
|
||||
REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
|
||||
@ -174,7 +176,7 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
|
||||
|
||||
if (Z_TYPE_PP(val) == IS_ARRAY) {
|
||||
myht = HASH_OF(*val);
|
||||
r = json_determine_array_type(val TSRMLS_CC);
|
||||
r = (options & PHP_JSON_FORCE_OBJECT) ? 1 : json_determine_array_type(val TSRMLS_CC);
|
||||
} else {
|
||||
myht = Z_OBJPROP_PP(val);
|
||||
r = 1;
|
||||
|
@ -8,8 +8,14 @@ json_encode() tests
|
||||
var_dump(json_encode(""));
|
||||
var_dump(json_encode(NULL));
|
||||
var_dump(json_encode(TRUE));
|
||||
|
||||
var_dump(json_encode(array(""=>"")));
|
||||
var_dump(json_encode(array(array(1))));
|
||||
var_dump(json_encode(array()));
|
||||
|
||||
var_dump(json_encode(array(""=>""), JSON_FORCE_OBJECT));
|
||||
var_dump(json_encode(array(array(1)), JSON_FORCE_OBJECT));
|
||||
var_dump(json_encode(array(), JSON_FORCE_OBJECT));
|
||||
|
||||
var_dump(json_encode(1));
|
||||
var_dump(json_encode("руссиш"));
|
||||
@ -23,6 +29,10 @@ string(4) "null"
|
||||
string(4) "true"
|
||||
string(7) "{"":""}"
|
||||
string(5) "[[1]]"
|
||||
string(2) "[]"
|
||||
string(7) "{"":""}"
|
||||
string(13) "{"0":{"0":1}}"
|
||||
string(2) "{}"
|
||||
string(1) "1"
|
||||
string(38) ""\u0440\u0443\u0441\u0441\u0438\u0448""
|
||||
Done
|
||||
|
Loading…
Reference in New Issue
Block a user