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.c162
1 files changed, 144 insertions, 18 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index 4e6753384..e8e19c79e 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -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);
@@ -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 */
@@ -49,6 +52,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
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)
@@ -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)
@@ -64,15 +68,34 @@ static const function_entry json_functions[] = {
};
/* }}} */
+/* {{{ 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);
@@ -81,6 +104,9 @@ static PHP_MINIT_FUNCTION(json)
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;
}
/* }}} */
@@ -89,6 +115,7 @@ static PHP_MINIT_FUNCTION(json)
*/
static PHP_GINIT_FUNCTION(json)
{
+ json_globals->encoder_depth = 0;
json_globals->error_code = 0;
}
/* }}} */
@@ -165,6 +192,30 @@ static int json_determine_array_type(zval **val TSRMLS_DC) /* {{{ */
}
/* }}} */
+/* {{{ 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;
@@ -190,6 +241,9 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
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)
@@ -217,10 +271,12 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
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) {
@@ -234,26 +290,36 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
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);
}
}
@@ -264,6 +330,10 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
}
}
}
+
+ --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, ']');
@@ -277,7 +347,7 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
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;
@@ -309,15 +379,14 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
}
}
-
- 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");
@@ -328,12 +397,15 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
}
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)
{
@@ -350,7 +422,11 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
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':
@@ -406,7 +482,7 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
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);
@@ -425,7 +501,43 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
}
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);
}
/* }}} */
@@ -470,8 +582,13 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
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;
@@ -485,7 +602,7 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
}
/* }}} */
-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;
@@ -511,7 +628,7 @@ PHP_JSON_API void php_json_decode(zval *return_value, char *str, int str_len, ze
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
@@ -554,6 +671,7 @@ PHP_JSON_API void php_json_decode(zval *return_value, char *str, int str_len, ze
}
/* }}} */
+
/* {{{ proto string json_encode(mixed data [, int options])
Returns the JSON representation of a value */
static PHP_FUNCTION(json_encode)
@@ -584,8 +702,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;
}
@@ -595,7 +714,14 @@ static PHP_FUNCTION(json_decode)
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);
}
/* }}} */