diff options
author | Sara Golemon <pollita@php.net> | 2010-05-21 22:59:58 +0000 |
---|---|---|
committer | Sara Golemon <pollita@php.net> | 2010-05-21 22:59:58 +0000 |
commit | 6eb4218433e5b2af2974648b31ca434b1aa4e314 (patch) | |
tree | 7107a79d39863c12df805228fc20dd27329c306c /ext/json/json.c | |
parent | a25482105ec71b40f14d1f6bf1ce0ee72622047b (diff) | |
download | php-git-6eb4218433e5b2af2974648b31ca434b1aa4e314.tar.gz |
Add JSON_BIGINT_AS_STRING for json_decode() to parse large numbers
as strings rather than casting to double and loosing precision.
Diffstat (limited to 'ext/json/json.c')
-rw-r--r-- | ext/json/json.c | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/ext/json/json.c b/ext/json/json.c index 93445d6b65..667774a911 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -52,6 +52,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1) ZEND_ARG_INFO(0, json) ZEND_ARG_INFO(0, assoc) ZEND_ARG_INFO(0, depth) + ZEND_ARG_INFO(0, options) ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_json_last_error, 0) @@ -99,6 +100,9 @@ static PHP_MINIT_FUNCTION(json) REGISTER_LONG_CONSTANT("JSON_ERROR_SYNTAX", PHP_JSON_ERROR_SYNTAX, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_ERROR_UTF8", PHP_JSON_ERROR_UTF8, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_OBJECT_AS_ARRAY", PHP_JSON_OBJECT_AS_ARRAY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_BIGINT_AS_STRING", PHP_JSON_BIGINT_AS_STRING, CONST_CS | CONST_PERSISTENT); + return SUCCESS; } /* }}} */ @@ -542,7 +546,7 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_ } /* }}} */ -PHP_JSON_API void php_json_decode(zval *return_value, char *str, int str_len, zend_bool assoc, long depth TSRMLS_DC) /* {{{ */ +PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len, int options, long depth TSRMLS_DC) /* {{{ */ { int utf16_len; zval *z; @@ -567,7 +571,7 @@ PHP_JSON_API void php_json_decode(zval *return_value, char *str, int str_len, ze ALLOC_INIT_ZVAL(z); jp = new_JSON_parser(depth); - if (parse_JSON(jp, z, utf16, utf16_len, assoc TSRMLS_CC)) { + if (parse_JSON_ex(jp, z, utf16, utf16_len, options TSRMLS_CC)) { *return_value = *z; } else @@ -610,6 +614,7 @@ PHP_JSON_API void php_json_decode(zval *return_value, char *str, int str_len, ze } /* }}} */ + /* {{{ proto string json_encode(mixed data [, int options]) Returns the JSON representation of a value */ static PHP_FUNCTION(json_encode) @@ -638,8 +643,9 @@ static PHP_FUNCTION(json_decode) int str_len; zend_bool assoc = 0; /* return JS objects as PHP objects by default */ long depth = JSON_PARSER_DEFAULT_DEPTH; + long options = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl", &str, &str_len, &assoc, &depth) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &str, &str_len, &assoc, &depth, &options) == FAILURE) { return; } @@ -647,7 +653,14 @@ static PHP_FUNCTION(json_decode) RETURN_NULL(); } - php_json_decode(return_value, str, str_len, assoc, depth TSRMLS_CC); + /* For BC reasons, the bool $assoc overrides the long $options bit for PHP_JSON_OBJECT_AS_ARRAY */ + if (assoc) { + options |= PHP_JSON_OBJECT_AS_ARRAY; + } else { + options &= ~PHP_JSON_OBJECT_AS_ARRAY; + } + + php_json_decode_ex(return_value, str, str_len, options, depth TSRMLS_CC); } /* }}} */ |