com php-src: Merge branch 'PHP-5.3' into PHP-5.4: ext/json/json.c
| From: | David Soria Parra | Date: | Tue, 20 Mar 2012 16:29:20 +0000 |
| Subject: | com php-src: Merge branch 'PHP-5.3' into PHP-5.4: ext/json/json.c | ||
| References: | 1 2 | Groups: | php.cvs |
| Request: | Send a blank email to php-cvs+get-68065@lists.php.net to get a copy of this message | ||
Commit: 71d894cd421d96517618e9d33dfea640499c8ffd
Author: David Soria Parra <dsp@php.net> Tue, 20 Mar 2012 17:29:20 +0100
Parents: 14ccdd1b556e5965bba9da11c584e9306f0349da 76ef76d2f875ccc3c9529565bdb29451a9b087ef
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=71d894cd421d96517618e9d33dfea640499c8ffd
Log:
Merge branch 'PHP-5.3' into PHP-5.4
* PHP-5.3:
Adding Braces in json.c to conform with coding standards
Changed paths:
MM ext/json/json.c
71d894cd421d96517618e9d33dfea640499c8ffd diff --combined ext/json/json.c index fb0e2d8,5b62c2f..fc1fcb7 --- a/ext/json/json.c +++ b/ext/json/json.c @@@ -29,7 -29,6 +29,7 @@@ #include "utf8_to_utf16.h" #include "JSON_parser.h" #include "php_json.h" +#include <zend_exceptions.h> static PHP_MINFO_FUNCTION(json); static PHP_FUNCTION(json_encode); @@@ -38,8 -37,6 +38,8 @@@ static PHP_FUNCTION(json_last_error) static const char digits[] = "0123456789abcdef"; +zend_class_entry *php_json_serializable_ce; + ZEND_DECLARE_MODULE_GLOBALS(json) /* {{{ arginfo */ @@@ -52,7 -49,6 +52,7 @@@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_dec 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) @@@ -60,7 -56,7 +60,7 @@@ ZEND_END_ARG_INFO( /* }}} */ /* {{{ json_functions[] */ -static const function_entry json_functions[] = { +static const zend_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) @@@ -68,34 -64,15 +68,34 @@@ }; /* }}} */ +/* {{{ JsonSerializable methods */ +ZEND_BEGIN_ARG_INFO(json_serialize_arginfo, 0) + /* No arguments */ +ZEND_END_ARG_INFO(); + +static const zend_function_entry json_serializable_interface[] = { + PHP_ABSTRACT_ME(JsonSerializable, jsonSerialize, json_serialize_arginfo) + PHP_FE_END +}; +/* }}} */ + /* {{{ MINIT */ 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); + 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); 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_FORCE_OBJECT", PHP_JSON_FORCE_OBJECT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_NUMERIC_CHECK", PHP_JSON_NUMERIC_CHECK, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_UNESCAPED_SLASHES", PHP_JSON_UNESCAPED_SLASHES, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE, 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); @@@ -104,9 -81,6 +104,9 @@@ 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; } /* }}} */ @@@ -115,7 -89,6 +115,7 @@@ */ static PHP_GINIT_FUNCTION(json) { + json_globals->encoder_depth = 0; json_globals->error_code = 0; } /* }}} */ @@@ -174,8 -147,9 +174,9 @@@ static int json_determine_array_type(zv 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_EXISTANT) + if (i == HASH_KEY_NON_EXISTANT) { break; + } if (i == HASH_KEY_IS_STRING) { return 1; @@@ -192,30 -166,6 +193,30 @@@ } /* }}} */ +/* {{{ 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; @@@ -241,9 -191,6 +242,9 @@@ smart_str_appendc(buf, '{'); } + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); + ++JSON_G(encoder_depth); + i = myht ? zend_hash_num_elements(myht) : 0; if (i > 0) @@@ -271,12 -218,10 +272,12 @@@ if (r == PHP_JSON_OUTPUT_ARRAY) { if (need_comma) { smart_str_appendc(buf, ','); + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); } else { need_comma = 1; } - + + 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) { @@@ -290,36 -235,26 +291,36 @@@ if (need_comma) { smart_str_appendc(buf, ','); + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); } else { need_comma = 1; } + 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, ','); + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); } else { need_comma = 1; } + 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); } } @@@ -330,10 -265,6 +331,10 @@@ } } } + + --JSON_G(encoder_depth); + 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, ']'); @@@ -347,7 -278,7 +348,7 @@@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */ { - int pos = 0; + int pos = 0, ulen = 0; unsigned short us; unsigned short *utf16; @@@ -379,14 -310,15 +380,14 @@@ } } - - utf16 = (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0); - - len = utf8_to_utf16(utf16, s, len); - if (len <= 0) { + + utf16 = (options & PHP_JSON_UNESCAPED_UNICODE) ? NULL : (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0); + ulen = utf8_to_utf16(utf16, s, len); + if (ulen <= 0) { if (utf16) { efree(utf16); } - if (len < 0) { + if (ulen < 0) { JSON_G(error_code) = PHP_JSON_ERROR_UTF8; if (!PG(display_errors)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument"); @@@ -397,15 -329,12 +398,15 @@@ } return; } + if (!(options & PHP_JSON_UNESCAPED_UNICODE)) { + len = ulen; + } smart_str_appendc(buf, '"'); while (pos < len) { - us = utf16[pos++]; + us = (options & PHP_JSON_UNESCAPED_UNICODE) ? s[pos++] : utf16[pos++]; switch (us) { @@@ -422,11 -351,7 +423,11 @@@ break; case '/': - smart_str_appendl(buf, "\\/", 2); + if (options & PHP_JSON_UNESCAPED_SLASHES) { + smart_str_appendc(buf, '/'); + } else { + smart_str_appendl(buf, "\\/", 2); + } break; case '\b': @@@ -482,7 -407,7 +483,7 @@@ break; default: - if (us >= ' ' && (us & 127) == us) { + if (us >= ' ' && ((options & PHP_JSON_UNESCAPED_UNICODE) || (us & 127) == us)) { smart_str_appendc(buf, (unsigned char) us); } else { smart_str_appendl(buf, "\\u", 2); @@@ -501,43 -426,7 +502,43 @@@ } smart_str_appendc(buf, '"'); - efree(utf16); + if (utf16) { + efree(utf16); + } +} +/* }}} */ + + +static void json_encode_serializable_object(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ +{ + zend_class_entry *ce = Z_OBJCE_P(val); + zval *retval = NULL, fname; + + 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); } /* }}} */ @@@ -582,13 -471,8 +583,13 @@@ PHP_JSON_API void php_json_encode(smart json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC); break; - case IS_ARRAY: 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; @@@ -602,7 -486,7 +603,7 @@@ } /* }}} */ -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; @@@ -628,7 -512,7 +629,7 @@@ 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 @@@ -671,7 -555,6 +672,7 @@@ } /* }}} */ + /* {{{ proto string json_encode(mixed data [, int options]) Returns the JSON representation of a value */ static PHP_FUNCTION(json_encode) @@@ -702,9 -585,8 +703,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; } @@@ -714,14 -596,7 +715,14 @@@ 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); } /* }}} */
71d894cd421d96517618e9d33dfea640499c8ffd diff --combined ext/json/json.c index fb0e2d8,5b62c2f..fc1fcb7 --- a/ext/json/json.c +++ b/ext/json/json.c @@@ -29,7 -29,6 +29,7 @@@ #include "utf8_to_utf16.h" #include "JSON_parser.h" #include "php_json.h" +#include <zend_exceptions.h> static PHP_MINFO_FUNCTION(json); static PHP_FUNCTION(json_encode); @@@ -38,8 -37,6 +38,8 @@@ static PHP_FUNCTION(json_last_error) static const char digits[] = "0123456789abcdef"; +zend_class_entry *php_json_serializable_ce; + ZEND_DECLARE_MODULE_GLOBALS(json) /* {{{ arginfo */ @@@ -52,7 -49,6 +52,7 @@@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_dec 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) @@@ -60,7 -56,7 +60,7 @@@ ZEND_END_ARG_INFO( /* }}} */ /* {{{ json_functions[] */ -static const function_entry json_functions[] = { +static const zend_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) @@@ -68,34 -64,15 +68,34 @@@ }; /* }}} */ +/* {{{ JsonSerializable methods */ +ZEND_BEGIN_ARG_INFO(json_serialize_arginfo, 0) + /* No arguments */ +ZEND_END_ARG_INFO(); + +static const zend_function_entry json_serializable_interface[] = { + PHP_ABSTRACT_ME(JsonSerializable, jsonSerialize, json_serialize_arginfo) + PHP_FE_END +}; +/* }}} */ + /* {{{ MINIT */ 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); + 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); 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_FORCE_OBJECT", PHP_JSON_FORCE_OBJECT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_NUMERIC_CHECK", PHP_JSON_NUMERIC_CHECK, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_UNESCAPED_SLASHES", PHP_JSON_UNESCAPED_SLASHES, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE, 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); @@@ -104,9 -81,6 +104,9 @@@ 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; } /* }}} */ @@@ -115,7 -89,6 +115,7 @@@ */ static PHP_GINIT_FUNCTION(json) { + json_globals->encoder_depth = 0; json_globals->error_code = 0; } /* }}} */ @@@ -174,8 -147,9 +174,9 @@@ static int json_determine_array_type(zv 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_EXISTANT) + if (i == HASH_KEY_NON_EXISTANT) { break; + } if (i == HASH_KEY_IS_STRING) { return 1; @@@ -192,30 -166,6 +193,30 @@@ } /* }}} */ +/* {{{ 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; @@@ -241,9 -191,6 +242,9 @@@ smart_str_appendc(buf, '{'); } + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); + ++JSON_G(encoder_depth); + i = myht ? zend_hash_num_elements(myht) : 0; if (i > 0) @@@ -271,12 -218,10 +272,12 @@@ if (r == PHP_JSON_OUTPUT_ARRAY) { if (need_comma) { smart_str_appendc(buf, ','); + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); } else { need_comma = 1; } - + + 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) { @@@ -290,36 -235,26 +291,36 @@@ if (need_comma) { smart_str_appendc(buf, ','); + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); } else { need_comma = 1; } + 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, ','); + json_pretty_print_char(buf, options, '\n' TSRMLS_CC); } else { need_comma = 1; } + 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); } } @@@ -330,10 -265,6 +331,10 @@@ } } } + + --JSON_G(encoder_depth); + 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, ']'); @@@ -347,7 -278,7 +348,7 @@@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */ { - int pos = 0; + int pos = 0, ulen = 0; unsigned short us; unsigned short *utf16; @@@ -379,14 -310,15 +380,14 @@@ } } - - utf16 = (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0); - - len = utf8_to_utf16(utf16, s, len); - if (len <= 0) { + + utf16 = (options & PHP_JSON_UNESCAPED_UNICODE) ? NULL : (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0); + ulen = utf8_to_utf16(utf16, s, len); + if (ulen <= 0) { if (utf16) { efree(utf16); } - if (len < 0) { + if (ulen < 0) { JSON_G(error_code) = PHP_JSON_ERROR_UTF8; if (!PG(display_errors)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument"); @@@ -397,15 -329,12 +398,15 @@@ } return; } + if (!(options & PHP_JSON_UNESCAPED_UNICODE)) { + len = ulen; + } smart_str_appendc(buf, '"'); while (pos < len) { - us = utf16[pos++]; + us = (options & PHP_JSON_UNESCAPED_UNICODE) ? s[pos++] : utf16[pos++]; switch (us) { @@@ -422,11 -351,7 +423,11 @@@ break; case '/': - smart_str_appendl(buf, "\\/", 2); + if (options & PHP_JSON_UNESCAPED_SLASHES) { + smart_str_appendc(buf, '/'); + } else { + smart_str_appendl(buf, "\\/", 2); + } break; case '\b': @@@ -482,7 -407,7 +483,7 @@@ break; default: - if (us >= ' ' && (us & 127) == us) { + if (us >= ' ' && ((options & PHP_JSON_UNESCAPED_UNICODE) || (us & 127) == us)) { smart_str_appendc(buf, (unsigned char) us); } else { smart_str_appendl(buf, "\\u", 2); @@@ -501,43 -426,7 +502,43 @@@ } smart_str_appendc(buf, '"'); - efree(utf16); + if (utf16) { + efree(utf16); + } +} +/* }}} */ + + +static void json_encode_serializable_object(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ +{ + zend_class_entry *ce = Z_OBJCE_P(val); + zval *retval = NULL, fname; + + 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); } /* }}} */ @@@ -582,13 -471,8 +583,13 @@@ PHP_JSON_API void php_json_encode(smart json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC); break; - case IS_ARRAY: 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; @@@ -602,7 -486,7 +603,7 @@@ } /* }}} */ -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; @@@ -628,7 -512,7 +629,7 @@@ 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 @@@ -671,7 -555,6 +672,7 @@@ } /* }}} */ + /* {{{ proto string json_encode(mixed data [, int options]) Returns the JSON representation of a value */ static PHP_FUNCTION(json_encode) @@@ -702,9 -585,8 +703,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; } @@@ -714,14 -596,7 +715,14 @@@ 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); } /* }}} */