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.c54
1 files changed, 32 insertions, 22 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index 01319d5f5b..219fd58cf5 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -33,14 +33,6 @@
#include "php_json_parser.h"
#include <zend_exceptions.h>
-#include <float.h>
-#if defined(DBL_MANT_DIG) && defined(DBL_MIN_EXP)
-#define NUM_BUF_SIZE (3 + DBL_MANT_DIG - DBL_MIN_EXP)
-#else
-#define NUM_BUF_SIZE 1080
-#endif
-
-
static PHP_MINFO_FUNCTION(json);
static PHP_FUNCTION(json_encode);
static PHP_FUNCTION(json_decode);
@@ -117,6 +109,7 @@ static PHP_MINIT_FUNCTION(json)
PHP_JSON_REGISTER_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE);
PHP_JSON_REGISTER_CONSTANT("JSON_PARTIAL_OUTPUT_ON_ERROR", PHP_JSON_PARTIAL_OUTPUT_ON_ERROR);
PHP_JSON_REGISTER_CONSTANT("JSON_PRESERVE_ZERO_FRACTION", PHP_JSON_PRESERVE_ZERO_FRACTION);
+ PHP_JSON_REGISTER_CONSTANT("JSON_UNESCAPED_LINE_TERMINATORS", PHP_JSON_UNESCAPED_LINE_TERMINATORS);
/* options for json_decode */
PHP_JSON_REGISTER_CONSTANT("JSON_OBJECT_AS_ARRAY", PHP_JSON_OBJECT_AS_ARRAY);
@@ -191,13 +184,23 @@ static PHP_MINFO_FUNCTION(json)
}
/* }}} */
-PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options) /* {{{ */
+PHP_JSON_API int php_json_encode(smart_str *buf, zval *val, int options) /* {{{ */
{
- php_json_encode_zval(buf, val, options);
+ php_json_encoder encoder;
+ int return_code;
+
+ php_json_encode_init(&encoder);
+ encoder.max_depth = JSON_G(encode_max_depth);
+ encoder.error_code = PHP_JSON_ERROR_NONE;
+
+ return_code = php_json_encode_zval(buf, val, options, &encoder);
+ JSON_G(error_code) = encoder.error_code;
+
+ return return_code;
}
/* }}} */
-PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth) /* {{{ */
+PHP_JSON_API int php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth) /* {{{ */
{
php_json_parser parser;
@@ -205,8 +208,11 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_l
if (php_json_yyparse(&parser)) {
JSON_G(error_code) = php_json_parser_error_code(&parser);
- RETURN_NULL();
+ RETVAL_NULL();
+ return FAILURE;
}
+
+ return SUCCESS;
}
/* }}} */
@@ -215,6 +221,7 @@ PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_l
static PHP_FUNCTION(json_encode)
{
zval *parameter;
+ php_json_encoder encoder;
smart_str buf = {0};
zend_long options = 0;
zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
@@ -223,19 +230,22 @@ static PHP_FUNCTION(json_encode)
return;
}
- JSON_G(error_code) = PHP_JSON_ERROR_NONE;
-
- JSON_G(encode_max_depth) = (int)depth;
-
- php_json_encode(&buf, parameter, (int)options);
+ php_json_encode_init(&encoder);
+ encoder.max_depth = (int)depth;
+ encoder.error_code = PHP_JSON_ERROR_NONE;
+ php_json_encode_zval(&buf, parameter, (int)options, &encoder);
+ JSON_G(error_code) = encoder.error_code;
- if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
+ if (encoder.error_code != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
smart_str_free(&buf);
- ZVAL_FALSE(return_value);
- } else {
- smart_str_0(&buf); /* copy? */
- ZVAL_NEW_STR(return_value, buf.s);
+ RETURN_FALSE;
+ }
+
+ smart_str_0(&buf); /* copy? */
+ if (buf.s) {
+ RETURN_NEW_STR(buf.s);
}
+ RETURN_EMPTY_STRING();
}
/* }}} */