static const char digits[] = "0123456789abcdef";
-static void php_json_escape_string(smart_str *buf, char *s, size_t len, int options);
+static int php_json_escape_string(smart_str *buf, char *s, size_t len, int options);
static int php_json_determine_array_type(zval *val) /* {{{ */
{
}
/* }}} */
-static void php_json_encode_array(smart_str *buf, zval *val, int options) /* {{{ */
+static int php_json_encode_array(smart_str *buf, zval *val, int options) /* {{{ */
{
int i, r, need_comma = 0;
HashTable *myht;
if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) {
JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
smart_str_appendl(buf, "null", 4);
- return;
+ return FAILURE;
}
if (r == PHP_JSON_OUTPUT_ARRAY) {
php_json_pretty_print_char(buf, options, '\n');
php_json_pretty_print_indent(buf, options);
- php_json_encode(buf, data, options);
} else if (r == PHP_JSON_OUTPUT_OBJECT) {
if (key) {
if (ZSTR_VAL(key)[0] == '\0' && ZSTR_LEN(key) > 0 && Z_TYPE_P(val) == IS_OBJECT) {
php_json_pretty_print_indent(buf, options);
php_json_escape_string(buf, ZSTR_VAL(key), ZSTR_LEN(key), options & ~PHP_JSON_NUMERIC_CHECK);
- smart_str_appendc(buf, ':');
-
- php_json_pretty_print_char(buf, options, ' ');
-
- php_json_encode(buf, data, options);
} else {
if (need_comma) {
smart_str_appendc(buf, ',');
smart_str_appendc(buf, '"');
smart_str_append_long(buf, (zend_long) index);
smart_str_appendc(buf, '"');
- smart_str_appendc(buf, ':');
+ }
- php_json_pretty_print_char(buf, options, ' ');
+ smart_str_appendc(buf, ':');
+ php_json_pretty_print_char(buf, options, ' ');
+ }
- php_json_encode(buf, data, options);
- }
+ if (php_json_encode(buf, data, options) == FAILURE && (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
+ return FAILURE;
}
if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) {
if (JSON_G(encoder_depth) > JSON_G(encode_max_depth)) {
JSON_G(error_code) = PHP_JSON_ERROR_DEPTH;
+ if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
+ return FAILURE;
+ }
}
--JSON_G(encoder_depth);
} else {
smart_str_appendc(buf, '}');
}
+
+ return SUCCESS;
}
/* }}} */
}
/* }}} */
-static void php_json_escape_string(smart_str *buf, char *s, size_t len, int options) /* {{{ */
+static int php_json_escape_string(smart_str *buf, char *s, size_t len, int options) /* {{{ */
{
int status;
unsigned int us;
if (len == 0) {
smart_str_appendl(buf, "\"\"", 2);
- return;
+ return SUCCESS;
}
if (options & PHP_JSON_NUMERIC_CHECK) {
if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) {
if (type == IS_LONG) {
smart_str_append_long(buf, p);
- return;
+ return SUCCESS;
} else if (type == IS_DOUBLE && php_json_is_valid_double(d)) {
php_json_encode_double(buf, d, options);
- return;
+ return SUCCESS;
}
}
/* validate UTF-8 string first */
if (php_json_utf8_to_utf16(NULL, s, len) < 0) {
JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
- smart_str_appendl(buf, "null", 4);
- return;
+ if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
+ smart_str_appendl(buf, "null", 4);
+ }
+ return FAILURE;
}
}
ZSTR_LEN(buf->s) = checkpoint;
}
JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
- smart_str_appendl(buf, "null", 4);
- return;
+ if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
+ smart_str_appendl(buf, "null", 4);
+ }
+ return FAILURE;
}
/* Escape U+2028/U+2029 line terminators, UNLESS both
JSON_UNESCAPED_UNICODE and
} while (pos < len);
smart_str_appendc(buf, '"');
+
+ return SUCCESS;
}
/* }}} */
-static void php_json_encode_serializable_object(smart_str *buf, zval *val, int options) /* {{{ */
+static int php_json_encode_serializable_object(smart_str *buf, zval *val, int options) /* {{{ */
{
zend_class_entry *ce = Z_OBJCE_P(val);
zval retval, fname;
if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) {
JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
- smart_str_appendl(buf, "null", 4);
- return;
+ if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
+ smart_str_appendl(buf, "null", 4);
+ }
+ return FAILURE;
}
origin_error_code = JSON_G(error_code);
if (FAILURE == call_user_function_ex(EG(function_table), val, &fname, &retval, 0, NULL, 1, NULL) || Z_TYPE(retval) == IS_UNDEF) {
zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ZSTR_VAL(ce->name));
- smart_str_appendl(buf, "null", sizeof("null") - 1);
zval_ptr_dtor(&fname);
- return;
+
+ if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
+ smart_str_appendl(buf, "null", 4);
+ }
+ return FAILURE;
}
JSON_G(error_code) = origin_error_code;
/* Error already raised */
zval_ptr_dtor(&retval);
zval_ptr_dtor(&fname);
- smart_str_appendl(buf, "null", sizeof("null") - 1);
- return;
+
+ if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
+ smart_str_appendl(buf, "null", 4);
+ }
+ return FAILURE;
}
if ((Z_TYPE(retval) == IS_OBJECT) &&
zval_ptr_dtor(&retval);
zval_ptr_dtor(&fname);
+
+ return SUCCESS;
}
/* }}} */
-void php_json_encode_zval(smart_str *buf, zval *val, int options) /* {{{ */
+int php_json_encode_zval(smart_str *buf, zval *val, int options) /* {{{ */
{
again:
switch (Z_TYPE_P(val))
break;
case IS_STRING:
- php_json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options);
- break;
+ return php_json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options);
case IS_OBJECT:
if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) {
- php_json_encode_serializable_object(buf, val, options);
- break;
+ return php_json_encode_serializable_object(buf, val, options);
}
/* fallthrough -- Non-serializable object */
case IS_ARRAY:
- php_json_encode_array(buf, val, options);
- break;
+ return php_json_encode_array(buf, val, options);
case IS_REFERENCE:
val = Z_REFVAL_P(val);
default:
JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
- smart_str_appendl(buf, "null", 4);
- break;
+ if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
+ smart_str_appendl(buf, "null", 4);
+ }
+ return FAILURE;
}
- return;
+ return SUCCESS;
}
/* }}} */