summaryrefslogtreecommitdiff
path: root/ext/json/json.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/json/json.c')
-rw-r--r--ext/json/json.c676
1 files changed, 42 insertions, 634 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index a28f99e10e..b40cb695d7 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
@@ -26,9 +26,10 @@
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/html.h"
-#include "ext/standard/php_smart_str.h"
-#include "JSON_parser.h"
+#include "zend_smart_str.h"
#include "php_json.h"
+#include "php_json_encoder.h"
+#include "php_json_parser.h"
#include <zend_exceptions.h>
#include <float.h>
@@ -45,8 +46,6 @@ static PHP_FUNCTION(json_decode);
static PHP_FUNCTION(json_last_error);
static PHP_FUNCTION(json_last_error_msg);
-static const char digits[] = "0123456789abcdef";
-
PHP_JSON_API zend_class_entry *php_json_serializable_ce;
ZEND_DECLARE_MODULE_GLOBALS(json)
@@ -99,7 +98,7 @@ static PHP_MINIT_FUNCTION(json)
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "JsonSerializable", json_serializable_interface);
- php_json_serializable_ce = zend_register_internal_interface(&ce TSRMLS_CC);
+ php_json_serializable_ce = zend_register_internal_interface(&ce);
REGISTER_LONG_CONSTANT("JSON_HEX_TAG", PHP_JSON_HEX_TAG, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("JSON_HEX_AMP", PHP_JSON_HEX_AMP, CONST_CS | CONST_PERSISTENT);
@@ -134,6 +133,9 @@ static PHP_MINIT_FUNCTION(json)
*/
static PHP_GINIT_FUNCTION(json)
{
+#if defined(COMPILE_DL_JSON) && defined(ZTS)
+ ZEND_TSRMLS_CACHE_UPDATE;
+#endif
json_globals->encoder_depth = 0;
json_globals->error_code = 0;
json_globals->encode_max_depth = 0;
@@ -162,6 +164,9 @@ zend_module_entry json_module_entry = {
/* }}} */
#ifdef COMPILE_DL_JSON
+#ifdef ZTS
+ZEND_TSRMLS_CACHE_DEFINE;
+#endif
ZEND_GET_MODULE(json)
#endif
@@ -176,649 +181,51 @@ static PHP_MINFO_FUNCTION(json)
}
/* }}} */
-static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC);
-
-static int json_determine_array_type(zval **val TSRMLS_DC) /* {{{ */
-{
- int i;
- HashTable *myht = HASH_OF(*val);
-
- i = myht ? zend_hash_num_elements(myht) : 0;
- if (i > 0) {
- char *key;
- ulong index, idx;
- uint key_len;
- HashPosition pos;
-
- zend_hash_internal_pointer_reset_ex(myht, &pos);
- idx = 0;
- for (;; zend_hash_move_forward_ex(myht, &pos)) {
- i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
- if (i == HASH_KEY_NON_EXISTENT) {
- break;
- }
-
- if (i == HASH_KEY_IS_STRING) {
- return 1;
- } else {
- if (index != idx) {
- return 1;
- }
- }
- idx++;
- }
- }
-
- return PHP_JSON_OUTPUT_ARRAY;
-}
-/* }}} */
-
-/* {{{ Pretty printing support functions */
-
-static inline void json_pretty_print_char(smart_str *buf, int options, char c TSRMLS_DC) /* {{{ */
-{
- if (options & PHP_JSON_PRETTY_PRINT) {
- smart_str_appendc(buf, c);
- }
-}
-/* }}} */
-
-static inline void json_pretty_print_indent(smart_str *buf, int options TSRMLS_DC) /* {{{ */
-{
- int i;
-
- if (options & PHP_JSON_PRETTY_PRINT) {
- for (i = 0; i < JSON_G(encoder_depth); ++i) {
- smart_str_appendl(buf, " ", 4);
- }
- }
-}
-/* }}} */
-
-/* }}} */
-
-static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) /* {{{ */
-{
- int i, r, need_comma = 0;
- HashTable *myht;
-
- if (Z_TYPE_PP(val) == IS_ARRAY) {
- myht = HASH_OF(*val);
- r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val TSRMLS_CC);
- } else {
- myht = Z_OBJPROP_PP(val);
- r = PHP_JSON_OUTPUT_OBJECT;
- }
-
- if (myht && myht->nApplyCount > 1) {
- JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
- smart_str_appendl(buf, "null", 4);
- return;
- }
-
- if (r == PHP_JSON_OUTPUT_ARRAY) {
- smart_str_appendc(buf, '[');
- } else {
- smart_str_appendc(buf, '{');
- }
-
- ++JSON_G(encoder_depth);
-
- i = myht ? zend_hash_num_elements(myht) : 0;
-
- if (i > 0)
- {
- char *key;
- zval **data;
- ulong index;
- uint key_len;
- HashPosition pos;
- HashTable *tmp_ht;
-
- zend_hash_internal_pointer_reset_ex(myht, &pos);
- for (;; zend_hash_move_forward_ex(myht, &pos)) {
- i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
- if (i == HASH_KEY_NON_EXISTENT)
- break;
-
- if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS) {
- tmp_ht = HASH_OF(*data);
- if (tmp_ht) {
- tmp_ht->nApplyCount++;
- }
-
- if (r == PHP_JSON_OUTPUT_ARRAY) {
- if (need_comma) {
- smart_str_appendc(buf, ',');
- } else {
- need_comma = 1;
- }
-
- json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
- json_pretty_print_indent(buf, options TSRMLS_CC);
- php_json_encode(buf, *data, options TSRMLS_CC);
- } else if (r == PHP_JSON_OUTPUT_OBJECT) {
- if (i == HASH_KEY_IS_STRING) {
- if (key[0] == '\0' && Z_TYPE_PP(val) == IS_OBJECT) {
- /* Skip protected and private members. */
- if (tmp_ht) {
- tmp_ht->nApplyCount--;
- }
- continue;
- }
-
- if (need_comma) {
- smart_str_appendc(buf, ',');
- } else {
- need_comma = 1;
- }
-
- json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
- json_pretty_print_indent(buf, options TSRMLS_CC);
-
- json_escape_string(buf, key, key_len - 1, options & ~PHP_JSON_NUMERIC_CHECK TSRMLS_CC);
- smart_str_appendc(buf, ':');
-
- json_pretty_print_char(buf, options, ' ' TSRMLS_CC);
-
- php_json_encode(buf, *data, options TSRMLS_CC);
- } else {
- if (need_comma) {
- smart_str_appendc(buf, ',');
- } else {
- need_comma = 1;
- }
-
- json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
- json_pretty_print_indent(buf, options TSRMLS_CC);
-
- smart_str_appendc(buf, '"');
- smart_str_append_long(buf, (long) index);
- smart_str_appendc(buf, '"');
- smart_str_appendc(buf, ':');
-
- json_pretty_print_char(buf, options, ' ' TSRMLS_CC);
-
- php_json_encode(buf, *data, options TSRMLS_CC);
- }
- }
-
- if (tmp_ht) {
- tmp_ht->nApplyCount--;
- }
- }
- }
- }
-
- if (JSON_G(encoder_depth) > JSON_G(encode_max_depth)) {
- JSON_G(error_code) = PHP_JSON_ERROR_DEPTH;
- }
- --JSON_G(encoder_depth);
-
- /* Only keep closing bracket on same line for empty arrays/objects */
- if (need_comma) {
- json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
- json_pretty_print_indent(buf, options TSRMLS_CC);
- }
-
- if (r == PHP_JSON_OUTPUT_ARRAY) {
- smart_str_appendc(buf, ']');
- } else {
- smart_str_appendc(buf, '}');
- }
-}
-/* }}} */
-
-static int json_utf8_to_utf16(unsigned short *utf16, char utf8[], int len) /* {{{ */
-{
- size_t pos = 0, us;
- int j, status;
-
- if (utf16) {
- /* really convert the utf8 string */
- for (j=0 ; pos < len ; j++) {
- us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status);
- if (status != SUCCESS) {
- return -1;
- }
- /* From http://en.wikipedia.org/wiki/UTF16 */
- if (us >= 0x10000) {
- us -= 0x10000;
- utf16[j++] = (unsigned short)((us >> 10) | 0xd800);
- utf16[j] = (unsigned short)((us & 0x3ff) | 0xdc00);
- } else {
- utf16[j] = (unsigned short)us;
- }
- }
- } else {
- /* Only check if utf8 string is valid, and compute utf16 length */
- for (j=0 ; pos < len ; j++) {
- us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status);
- if (status != SUCCESS) {
- return -1;
- }
- if (us >= 0x10000) {
- j++;
- }
- }
- }
- return j;
-}
-/* }}} */
-
-
-static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */
+PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options) /* {{{ */
{
- int pos = 0, ulen = 0;
- unsigned short us;
- unsigned short *utf16;
- size_t newlen;
-
- if (len == 0) {
- smart_str_appendl(buf, "\"\"", 2);
- return;
- }
-
- if (options & PHP_JSON_NUMERIC_CHECK) {
- double d;
- int type;
- long p;
-
- if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) {
- if (type == IS_LONG) {
- smart_str_append_long(buf, p);
- } else if (type == IS_DOUBLE) {
- if (!zend_isinf(d) && !zend_isnan(d)) {
- char num[NUM_BUF_SIZE];
- int l;
-
- php_gcvt(d, EG(precision), '.', 'e', (char *)num);
- l = strlen(num);
- if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && l < NUM_BUF_SIZE - 2) {
- num[l++] = '.';
- num[l++] = '0';
- num[l] = '\0';
- }
- smart_str_appendl(buf, num, l);
- } else {
- JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
- smart_str_appendc(buf, '0');
- }
- }
- return;
- }
-
- }
-
- utf16 = (options & PHP_JSON_UNESCAPED_UNICODE) ? NULL : (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0);
- ulen = json_utf8_to_utf16(utf16, s, len);
- if (ulen <= 0) {
- if (utf16) {
- efree(utf16);
- }
- if (ulen < 0) {
- JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
- smart_str_appendl(buf, "null", 4);
- } else {
- smart_str_appendl(buf, "\"\"", 2);
- }
- return;
- }
- if (!(options & PHP_JSON_UNESCAPED_UNICODE)) {
- len = ulen;
- }
-
- /* pre-allocate for string length plus 2 quotes */
- smart_str_alloc(buf, len+2, 0);
- smart_str_appendc(buf, '"');
-
- while (pos < len)
- {
- us = (options & PHP_JSON_UNESCAPED_UNICODE) ? s[pos++] : utf16[pos++];
-
- switch (us)
- {
- case '"':
- if (options & PHP_JSON_HEX_QUOT) {
- smart_str_appendl(buf, "\\u0022", 6);
- } else {
- smart_str_appendl(buf, "\\\"", 2);
- }
- break;
-
- case '\\':
- smart_str_appendl(buf, "\\\\", 2);
- break;
-
- case '/':
- if (options & PHP_JSON_UNESCAPED_SLASHES) {
- smart_str_appendc(buf, '/');
- } else {
- smart_str_appendl(buf, "\\/", 2);
- }
- break;
-
- case '\b':
- smart_str_appendl(buf, "\\b", 2);
- break;
-
- case '\f':
- smart_str_appendl(buf, "\\f", 2);
- break;
-
- case '\n':
- smart_str_appendl(buf, "\\n", 2);
- break;
-
- case '\r':
- smart_str_appendl(buf, "\\r", 2);
- break;
-
- case '\t':
- smart_str_appendl(buf, "\\t", 2);
- break;
-
- case '<':
- if (options & PHP_JSON_HEX_TAG) {
- smart_str_appendl(buf, "\\u003C", 6);
- } else {
- smart_str_appendc(buf, '<');
- }
- break;
-
- case '>':
- if (options & PHP_JSON_HEX_TAG) {
- smart_str_appendl(buf, "\\u003E", 6);
- } else {
- smart_str_appendc(buf, '>');
- }
- break;
-
- case '&':
- if (options & PHP_JSON_HEX_AMP) {
- smart_str_appendl(buf, "\\u0026", 6);
- } else {
- smart_str_appendc(buf, '&');
- }
- break;
-
- case '\'':
- if (options & PHP_JSON_HEX_APOS) {
- smart_str_appendl(buf, "\\u0027", 6);
- } else {
- smart_str_appendc(buf, '\'');
- }
- break;
-
- default:
- if (us >= ' ' && ((options & PHP_JSON_UNESCAPED_UNICODE) || (us & 127) == us)) {
- smart_str_appendc(buf, (unsigned char) us);
- } else {
- smart_str_appendl(buf, "\\u", 2);
- smart_str_appendc(buf, digits[(us & 0xf000) >> 12]);
- smart_str_appendc(buf, digits[(us & 0xf00) >> 8]);
- smart_str_appendc(buf, digits[(us & 0xf0) >> 4]);
- smart_str_appendc(buf, digits[(us & 0xf)]);
- }
- break;
- }
- }
-
- smart_str_appendc(buf, '"');
- if (utf16) {
- efree(utf16);
- }
+ php_json_encode_zval(buf, val, options);
}
/* }}} */
-
-static void json_encode_serializable_object(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */
+PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth) /* {{{ */
{
- zend_class_entry *ce = Z_OBJCE_P(val);
- zval *retval = NULL, fname;
- HashTable* myht;
+ php_json_parser parser;
- if (Z_TYPE_P(val) == IS_ARRAY) {
- myht = HASH_OF(val);
- } else {
- myht = Z_OBJPROP_P(val);
- }
+ php_json_parser_init(&parser, return_value, str, str_len, (int)options, (int)depth);
- if (myht && myht->nApplyCount > 1) {
- JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
- smart_str_appendl(buf, "null", 4);
- return;
- }
-
- ZVAL_STRING(&fname, "jsonSerialize", 0);
-
- if (FAILURE == call_user_function_ex(EG(function_table), &val, &fname, &retval, 0, NULL, 1, NULL TSRMLS_CC) || !retval) {
- zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed calling %s::jsonSerialize()", ce->name);
- smart_str_appendl(buf, "null", sizeof("null") - 1);
- return;
- }
-
- if (EG(exception)) {
- /* Error already raised */
- zval_ptr_dtor(&retval);
- smart_str_appendl(buf, "null", sizeof("null") - 1);
- return;
- }
-
- if ((Z_TYPE_P(retval) == IS_OBJECT) &&
- (Z_OBJ_HANDLE_P(retval) == Z_OBJ_HANDLE_P(val))) {
- /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */
- json_encode_array(buf, &retval, options TSRMLS_CC);
- } else {
- /* All other types, encode as normal */
- php_json_encode(buf, retval, options TSRMLS_CC);
- }
-
- zval_ptr_dtor(&retval);
-}
-/* }}} */
-
-PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */
-{
- switch (Z_TYPE_P(val))
- {
- case IS_NULL:
- smart_str_appendl(buf, "null", 4);
- break;
-
- case IS_BOOL:
- if (Z_BVAL_P(val)) {
- smart_str_appendl(buf, "true", 4);
- } else {
- smart_str_appendl(buf, "false", 5);
- }
- break;
-
- case IS_LONG:
- smart_str_append_long(buf, Z_LVAL_P(val));
- break;
-
- case IS_DOUBLE:
- {
- char num[NUM_BUF_SIZE];
- int len;
- double dbl = Z_DVAL_P(val);
-
- if (!zend_isinf(dbl) && !zend_isnan(dbl)) {
- php_gcvt(dbl, EG(precision), '.', 'e', (char *)num);
- len = strlen(num);
- if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < NUM_BUF_SIZE - 2) {
- num[len++] = '.';
- num[len++] = '0';
- num[len] = '\0';
- }
- smart_str_appendl(buf, num, len);
- } else {
- JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
- smart_str_appendc(buf, '0');
- }
- }
- break;
-
- case IS_STRING:
- json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC);
- break;
-
- case IS_OBJECT:
- if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce TSRMLS_CC)) {
- json_encode_serializable_object(buf, val, options TSRMLS_CC);
- break;
- }
- /* fallthrough -- Non-serializable object */
- case IS_ARRAY:
- json_encode_array(buf, &val, options TSRMLS_CC);
- break;
-
- default:
- JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
- smart_str_appendl(buf, "null", 4);
- break;
- }
-
- return;
-}
-/* }}} */
-
-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;
- unsigned short *utf16;
- JSON_parser jp;
-
- utf16 = (unsigned short *) safe_emalloc((str_len+1), sizeof(unsigned short), 1);
-
- utf16_len = json_utf8_to_utf16(utf16, str, str_len);
- if (utf16_len <= 0) {
- if (utf16) {
- efree(utf16);
- }
- JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
- RETURN_NULL();
- }
-
- if (depth <= 0) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Depth must be greater than zero");
- efree(utf16);
+ if (php_json_yyparse(&parser)) {
+ JSON_G(error_code) = php_json_parser_error_code(&parser);
RETURN_NULL();
}
-
- ALLOC_INIT_ZVAL(z);
- jp = new_JSON_parser(depth);
- if (parse_JSON_ex(jp, z, utf16, utf16_len, options TSRMLS_CC)) {
- *return_value = *z;
- }
- else
- {
- double d;
- int type, overflow_info;
- long p;
- char *trim = str;
- int trim_len = str_len;
-
- /* Increment trimmed string pointer to strip leading whitespace */
- /* JSON RFC says to consider as whitespace: space, tab, LF or CR */
- while (trim_len && (*trim == ' ' || *trim == '\t' || *trim == '\n' || *trim == '\r')) {
- trim++;
- trim_len--;
- }
-
- /* Decrement trimmed string length to strip trailing whitespace */
- while (trim_len && (trim[trim_len - 1] == ' ' || trim[trim_len - 1] == '\t' || trim[trim_len - 1] == '\n' || trim[trim_len - 1] == '\r')) {
- trim_len--;
- }
-
- RETVAL_NULL();
- if (trim_len == 4) {
- if (!strncmp(trim, "null", trim_len)) {
- /* We need to explicitly clear the error because its an actual NULL and not an error */
- jp->error_code = PHP_JSON_ERROR_NONE;
- RETVAL_NULL();
- } else if (!strncmp(trim, "true", trim_len)) {
- RETVAL_BOOL(1);
- }
- } else if (trim_len == 5 && !strncmp(trim, "false", trim_len)) {
- RETVAL_BOOL(0);
- }
-
- if ((type = is_numeric_string_ex(trim, trim_len, &p, &d, 0, &overflow_info)) != 0) {
- if (type == IS_LONG) {
- RETVAL_LONG(p);
- } else if (type == IS_DOUBLE) {
- if (options & PHP_JSON_BIGINT_AS_STRING && overflow_info) {
- /* Within an object or array, a numeric literal is assumed
- * to be an integer if and only if it's entirely made up of
- * digits (exponent notation will result in the number
- * being treated as a double). We'll match that behaviour
- * here. */
- int i;
- zend_bool is_float = 0;
-
- for (i = (trim[0] == '-' ? 1 : 0); i < trim_len; i++) {
- /* Not using isdigit() because it's locale specific,
- * but we expect JSON input to always be UTF-8. */
- if (trim[i] < '0' || trim[i] > '9') {
- is_float = 1;
- break;
- }
- }
-
- if (is_float) {
- RETVAL_DOUBLE(d);
- } else {
- RETVAL_STRINGL(trim, trim_len, 1);
- }
- } else {
- RETVAL_DOUBLE(d);
- }
- }
- }
-
- if (Z_TYPE_P(return_value) != IS_NULL) {
- jp->error_code = PHP_JSON_ERROR_NONE;
- }
-
- zval_dtor(z);
- }
- FREE_ZVAL(z);
- efree(utf16);
- JSON_G(error_code) = jp->error_code;
- free_JSON_parser(jp);
}
/* }}} */
-
/* {{{ proto string json_encode(mixed data [, int options[, int depth]])
Returns the JSON representation of a value */
static PHP_FUNCTION(json_encode)
{
zval *parameter;
smart_str buf = {0};
- long options = 0;
- long depth = JSON_PARSER_DEFAULT_DEPTH;
+ zend_long options = 0;
+ zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &parameter, &options, &depth) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|ll", &parameter, &options, &depth) == FAILURE) {
return;
}
JSON_G(error_code) = PHP_JSON_ERROR_NONE;
- JSON_G(encode_max_depth) = depth;
+ JSON_G(encode_max_depth) = (int)depth;
- php_json_encode(&buf, parameter, options TSRMLS_CC);
+ php_json_encode(&buf, parameter, (int)options);
if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
+ smart_str_free(&buf);
ZVAL_FALSE(return_value);
} else {
- ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
+ smart_str_0(&buf); /* copy? */
+ ZVAL_NEW_STR(return_value, buf.s);
}
-
- smart_str_free(&buf);
}
/* }}} */
@@ -827,18 +234,19 @@ static PHP_FUNCTION(json_encode)
static PHP_FUNCTION(json_decode)
{
char *str;
- int str_len;
+ size_t str_len;
zend_bool assoc = 0; /* return JS objects as PHP objects by default */
- long depth = JSON_PARSER_DEFAULT_DEPTH;
- long options = 0;
+ zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
+ zend_long options = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &str, &str_len, &assoc, &depth, &options) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|bll", &str, &str_len, &assoc, &depth, &options) == FAILURE) {
return;
}
JSON_G(error_code) = 0;
if (!str_len) {
+ JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX;
RETURN_NULL();
}
@@ -849,7 +257,7 @@ static PHP_FUNCTION(json_decode)
options &= ~PHP_JSON_OBJECT_AS_ARRAY;
}
- php_json_decode_ex(return_value, str, str_len, options, depth TSRMLS_CC);
+ php_json_decode_ex(return_value, str, str_len, options, depth);
}
/* }}} */
@@ -875,25 +283,25 @@ static PHP_FUNCTION(json_last_error_msg)
switch(JSON_G(error_code)) {
case PHP_JSON_ERROR_NONE:
- RETURN_STRING("No error", 1);
+ RETURN_STRING("No error");
case PHP_JSON_ERROR_DEPTH:
- RETURN_STRING("Maximum stack depth exceeded", 1);
+ RETURN_STRING("Maximum stack depth exceeded");
case PHP_JSON_ERROR_STATE_MISMATCH:
- RETURN_STRING("State mismatch (invalid or malformed JSON)", 1);
+ RETURN_STRING("State mismatch (invalid or malformed JSON)");
case PHP_JSON_ERROR_CTRL_CHAR:
- RETURN_STRING("Control character error, possibly incorrectly encoded", 1);
+ RETURN_STRING("Control character error, possibly incorrectly encoded");
case PHP_JSON_ERROR_SYNTAX:
- RETURN_STRING("Syntax error", 1);
+ RETURN_STRING("Syntax error");
case PHP_JSON_ERROR_UTF8:
- RETURN_STRING("Malformed UTF-8 characters, possibly incorrectly encoded", 1);
+ RETURN_STRING("Malformed UTF-8 characters, possibly incorrectly encoded");
case PHP_JSON_ERROR_RECURSION:
- RETURN_STRING("Recursion detected", 1);
+ RETURN_STRING("Recursion detected");
case PHP_JSON_ERROR_INF_OR_NAN:
- RETURN_STRING("Inf and NaN cannot be JSON encoded", 1);
+ RETURN_STRING("Inf and NaN cannot be JSON encoded");
case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
- RETURN_STRING("Type is not supported", 1);
+ RETURN_STRING("Type is not supported");
default:
- RETURN_STRING("Unknown error", 1);
+ RETURN_STRING("Unknown error");
}
}