summaryrefslogtreecommitdiff
path: root/ext/json/json.c
diff options
context:
space:
mode:
authorScott MacVicar <scottmac@php.net>2008-12-19 02:00:57 +0000
committerScott MacVicar <scottmac@php.net>2008-12-19 02:00:57 +0000
commitfdf2d1e46f4b6bd08e600b08b8d29ac7f694d2cf (patch)
tree5fa918740a3b319290d04df43367084abfcd3421 /ext/json/json.c
parentc310143ac7b06b7e778a99d26ee2690c7e4ea33c (diff)
downloadphp-git-fdf2d1e46f4b6bd08e600b08b8d29ac7f694d2cf.tar.gz
MFH Add json_last_error() for getting a bit of information about what failed during a decode, also fixes a segfault when we have [1}
[DOC]
Diffstat (limited to 'ext/json/json.c')
-rw-r--r--ext/json/json.c49
1 files changed, 40 insertions, 9 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index e09027576b..b3049d002d 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -33,6 +33,7 @@
static PHP_MINFO_FUNCTION(json);
static PHP_FUNCTION(json_encode);
static PHP_FUNCTION(json_decode);
+static PHP_FUNCTION(json_last_error);
static const char digits[] = "0123456789abcdef";
@@ -41,6 +42,8 @@ static const char digits[] = "0123456789abcdef";
#define PHP_JSON_HEX_APOS (1<<2)
#define PHP_JSON_HEX_QUOT (1<<3)
+ZEND_DECLARE_MODULE_GLOBALS(json)
+
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_json_encode, 0, 0, 1)
ZEND_ARG_INFO(0, value)
@@ -51,15 +54,16 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
ZEND_ARG_INFO(0, json)
ZEND_ARG_INFO(0, assoc)
ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO(arginfo_json_last_error, 0)
+ZEND_END_ARG_INFO()
/* }}} */
-/* {{{ json_functions[]
- *
- * Every user visible function must have an entry in json_functions[].
- */
+/* {{{ json_functions[] */
static const function_entry json_functions[] = {
PHP_FE(json_encode, arginfo_json_encode)
PHP_FE(json_decode, arginfo_json_decode)
+ PHP_FE(json_last_error, arginfo_json_last_error)
{NULL, NULL, NULL}
};
/* }}} */
@@ -72,17 +76,29 @@ static PHP_MINIT_FUNCTION(json)
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_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("JSON_ERROR_STATE_MISMATCH", PHP_JSON_ERROR_STATE_MISMATCH, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("JSON_ERROR_CTRL_CHAR", PHP_JSON_ERROR_CTRL_CHAR, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("JSON_ERROR_SYNTAX", PHP_JSON_ERROR_SYNTAX, CONST_CS | CONST_PERSISTENT);
+
return SUCCESS;
}
/* }}} */
+/* {{{ PHP_GINIT_FUNCTION
+*/
+static PHP_GINIT_FUNCTION(json)
+{
+ json_globals->error_code = 0;
+}
+/* }}} */
+
/* {{{ json_module_entry
*/
zend_module_entry json_module_entry = {
-#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
-#endif
"json",
json_functions,
PHP_MINIT(json),
@@ -90,10 +106,12 @@ zend_module_entry json_module_entry = {
NULL,
NULL,
PHP_MINFO(json),
-#if ZEND_MODULE_API_NO >= 20010901
PHP_JSON_VERSION,
-#endif
- STANDARD_MODULE_PROPERTIES
+ PHP_MODULE_GLOBALS(json),
+ PHP_GINIT(json),
+ NULL,
+ NULL,
+ STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
@@ -533,10 +551,23 @@ static PHP_FUNCTION(json_decode)
}
FREE_ZVAL(z);
efree(utf16);
+ JSON_G(error_code) = jp->error_code;
free_JSON_parser(jp);
}
/* }}} */
+/* {{{ proto int json_last_error()
+ Returns the error code of the last json_decode(). */
+static PHP_FUNCTION(json_last_error)
+{
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+
+ RETURN_LONG(JSON_G(error_code));
+}
+/* }}} */
+
/*
* Local variables:
* tab-width: 4