Annotation of embedaddon/php/ext/json/json.c, revision 1.1.1.2
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
5: | Copyright (c) 1997-2012 The PHP Group |
6: +----------------------------------------------------------------------+
7: | This source file is subject to version 3.01 of the PHP license, |
8: | that is bundled with this package in the file LICENSE, and is |
9: | available through the world-wide-web at the following url: |
10: | http://www.php.net/license/3_01.txt |
11: | If you did not receive a copy of the PHP license and are unable to |
12: | obtain it through the world-wide-web, please send a note to |
13: | license@php.net so we can mail you a copy immediately. |
14: +----------------------------------------------------------------------+
15: | Author: Omar Kilani <omar@php.net> |
16: +----------------------------------------------------------------------+
17: */
18:
1.1.1.2 ! misho 19: /* $Id$ */
1.1 misho 20:
21: #ifdef HAVE_CONFIG_H
22: #include "config.h"
23: #endif
24:
25: #include "php.h"
26: #include "php_ini.h"
27: #include "ext/standard/info.h"
28: #include "ext/standard/php_smart_str.h"
29: #include "utf8_to_utf16.h"
30: #include "JSON_parser.h"
31: #include "php_json.h"
1.1.1.2 ! misho 32: #include <zend_exceptions.h>
1.1 misho 33:
34: static PHP_MINFO_FUNCTION(json);
35: static PHP_FUNCTION(json_encode);
36: static PHP_FUNCTION(json_decode);
37: static PHP_FUNCTION(json_last_error);
38:
39: static const char digits[] = "0123456789abcdef";
40:
1.1.1.2 ! misho 41: zend_class_entry *php_json_serializable_ce;
! 42:
1.1 misho 43: ZEND_DECLARE_MODULE_GLOBALS(json)
44:
45: /* {{{ arginfo */
46: ZEND_BEGIN_ARG_INFO_EX(arginfo_json_encode, 0, 0, 1)
47: ZEND_ARG_INFO(0, value)
48: ZEND_ARG_INFO(0, options)
49: ZEND_END_ARG_INFO()
50:
51: ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
52: ZEND_ARG_INFO(0, json)
53: ZEND_ARG_INFO(0, assoc)
54: ZEND_ARG_INFO(0, depth)
1.1.1.2 ! misho 55: ZEND_ARG_INFO(0, options)
1.1 misho 56: ZEND_END_ARG_INFO()
57:
58: ZEND_BEGIN_ARG_INFO(arginfo_json_last_error, 0)
59: ZEND_END_ARG_INFO()
60: /* }}} */
61:
62: /* {{{ json_functions[] */
1.1.1.2 ! misho 63: static const zend_function_entry json_functions[] = {
1.1 misho 64: PHP_FE(json_encode, arginfo_json_encode)
65: PHP_FE(json_decode, arginfo_json_decode)
66: PHP_FE(json_last_error, arginfo_json_last_error)
67: PHP_FE_END
68: };
69: /* }}} */
70:
1.1.1.2 ! misho 71: /* {{{ JsonSerializable methods */
! 72: ZEND_BEGIN_ARG_INFO(json_serialize_arginfo, 0)
! 73: /* No arguments */
! 74: ZEND_END_ARG_INFO();
! 75:
! 76: static const zend_function_entry json_serializable_interface[] = {
! 77: PHP_ABSTRACT_ME(JsonSerializable, jsonSerialize, json_serialize_arginfo)
! 78: PHP_FE_END
! 79: };
! 80: /* }}} */
! 81:
1.1 misho 82: /* {{{ MINIT */
83: static PHP_MINIT_FUNCTION(json)
84: {
1.1.1.2 ! misho 85: zend_class_entry ce;
! 86:
! 87: INIT_CLASS_ENTRY(ce, "JsonSerializable", json_serializable_interface);
! 88: php_json_serializable_ce = zend_register_internal_interface(&ce TSRMLS_CC);
! 89:
1.1 misho 90: REGISTER_LONG_CONSTANT("JSON_HEX_TAG", PHP_JSON_HEX_TAG, CONST_CS | CONST_PERSISTENT);
91: REGISTER_LONG_CONSTANT("JSON_HEX_AMP", PHP_JSON_HEX_AMP, CONST_CS | CONST_PERSISTENT);
92: REGISTER_LONG_CONSTANT("JSON_HEX_APOS", PHP_JSON_HEX_APOS, CONST_CS | CONST_PERSISTENT);
93: REGISTER_LONG_CONSTANT("JSON_HEX_QUOT", PHP_JSON_HEX_QUOT, CONST_CS | CONST_PERSISTENT);
94: REGISTER_LONG_CONSTANT("JSON_FORCE_OBJECT", PHP_JSON_FORCE_OBJECT, CONST_CS | CONST_PERSISTENT);
95: REGISTER_LONG_CONSTANT("JSON_NUMERIC_CHECK", PHP_JSON_NUMERIC_CHECK, CONST_CS | CONST_PERSISTENT);
1.1.1.2 ! misho 96: REGISTER_LONG_CONSTANT("JSON_UNESCAPED_SLASHES", PHP_JSON_UNESCAPED_SLASHES, CONST_CS | CONST_PERSISTENT);
! 97: REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, CONST_CS | CONST_PERSISTENT);
! 98: REGISTER_LONG_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE, CONST_CS | CONST_PERSISTENT);
1.1 misho 99:
100: REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT);
101: REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT);
102: REGISTER_LONG_CONSTANT("JSON_ERROR_STATE_MISMATCH", PHP_JSON_ERROR_STATE_MISMATCH, CONST_CS | CONST_PERSISTENT);
103: REGISTER_LONG_CONSTANT("JSON_ERROR_CTRL_CHAR", PHP_JSON_ERROR_CTRL_CHAR, CONST_CS | CONST_PERSISTENT);
104: REGISTER_LONG_CONSTANT("JSON_ERROR_SYNTAX", PHP_JSON_ERROR_SYNTAX, CONST_CS | CONST_PERSISTENT);
105: REGISTER_LONG_CONSTANT("JSON_ERROR_UTF8", PHP_JSON_ERROR_UTF8, CONST_CS | CONST_PERSISTENT);
106:
1.1.1.2 ! misho 107: REGISTER_LONG_CONSTANT("JSON_OBJECT_AS_ARRAY", PHP_JSON_OBJECT_AS_ARRAY, CONST_CS | CONST_PERSISTENT);
! 108: REGISTER_LONG_CONSTANT("JSON_BIGINT_AS_STRING", PHP_JSON_BIGINT_AS_STRING, CONST_CS | CONST_PERSISTENT);
! 109:
1.1 misho 110: return SUCCESS;
111: }
112: /* }}} */
113:
114: /* {{{ PHP_GINIT_FUNCTION
115: */
116: static PHP_GINIT_FUNCTION(json)
117: {
1.1.1.2 ! misho 118: json_globals->encoder_depth = 0;
1.1 misho 119: json_globals->error_code = 0;
120: }
121: /* }}} */
122:
123:
124: /* {{{ json_module_entry
125: */
126: zend_module_entry json_module_entry = {
127: STANDARD_MODULE_HEADER,
128: "json",
129: json_functions,
130: PHP_MINIT(json),
131: NULL,
132: NULL,
133: NULL,
134: PHP_MINFO(json),
135: PHP_JSON_VERSION,
136: PHP_MODULE_GLOBALS(json),
137: PHP_GINIT(json),
138: NULL,
139: NULL,
140: STANDARD_MODULE_PROPERTIES_EX
141: };
142: /* }}} */
143:
144: #ifdef COMPILE_DL_JSON
145: ZEND_GET_MODULE(json)
146: #endif
147:
148: /* {{{ PHP_MINFO_FUNCTION
149: */
150: static PHP_MINFO_FUNCTION(json)
151: {
152: php_info_print_table_start();
153: php_info_print_table_row(2, "json support", "enabled");
154: php_info_print_table_row(2, "json version", PHP_JSON_VERSION);
155: php_info_print_table_end();
156: }
157: /* }}} */
158:
159: static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC);
160:
161: static int json_determine_array_type(zval **val TSRMLS_DC) /* {{{ */
162: {
163: int i;
164: HashTable *myht = HASH_OF(*val);
165:
166: i = myht ? zend_hash_num_elements(myht) : 0;
167: if (i > 0) {
168: char *key;
169: ulong index, idx;
170: uint key_len;
171: HashPosition pos;
172:
173: zend_hash_internal_pointer_reset_ex(myht, &pos);
174: idx = 0;
175: for (;; zend_hash_move_forward_ex(myht, &pos)) {
176: i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
1.1.1.2 ! misho 177: if (i == HASH_KEY_NON_EXISTANT) {
1.1 misho 178: break;
1.1.1.2 ! misho 179: }
1.1 misho 180:
181: if (i == HASH_KEY_IS_STRING) {
182: return 1;
183: } else {
184: if (index != idx) {
185: return 1;
186: }
187: }
188: idx++;
189: }
190: }
191:
192: return PHP_JSON_OUTPUT_ARRAY;
193: }
194: /* }}} */
195:
1.1.1.2 ! misho 196: /* {{{ Pretty printing support functions */
! 197:
! 198: static inline void json_pretty_print_char(smart_str *buf, int options, char c TSRMLS_DC) /* {{{ */
! 199: {
! 200: if (options & PHP_JSON_PRETTY_PRINT) {
! 201: smart_str_appendc(buf, c);
! 202: }
! 203: }
! 204: /* }}} */
! 205:
! 206: static inline void json_pretty_print_indent(smart_str *buf, int options TSRMLS_DC) /* {{{ */
! 207: {
! 208: int i;
! 209:
! 210: if (options & PHP_JSON_PRETTY_PRINT) {
! 211: for (i = 0; i < JSON_G(encoder_depth); ++i) {
! 212: smart_str_appendl(buf, " ", 4);
! 213: }
! 214: }
! 215: }
! 216: /* }}} */
! 217:
! 218: /* }}} */
! 219:
1.1 misho 220: static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) /* {{{ */
221: {
222: int i, r;
223: HashTable *myht;
224:
225: if (Z_TYPE_PP(val) == IS_ARRAY) {
226: myht = HASH_OF(*val);
227: r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val TSRMLS_CC);
228: } else {
229: myht = Z_OBJPROP_PP(val);
230: r = PHP_JSON_OUTPUT_OBJECT;
231: }
232:
233: if (myht && myht->nApplyCount > 1) {
234: php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
235: smart_str_appendl(buf, "null", 4);
236: return;
237: }
238:
239: if (r == PHP_JSON_OUTPUT_ARRAY) {
240: smart_str_appendc(buf, '[');
241: } else {
242: smart_str_appendc(buf, '{');
243: }
244:
1.1.1.2 ! misho 245: json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
! 246: ++JSON_G(encoder_depth);
! 247:
1.1 misho 248: i = myht ? zend_hash_num_elements(myht) : 0;
249:
250: if (i > 0)
251: {
252: char *key;
253: zval **data;
254: ulong index;
255: uint key_len;
256: HashPosition pos;
257: HashTable *tmp_ht;
258: int need_comma = 0;
259:
260: zend_hash_internal_pointer_reset_ex(myht, &pos);
261: for (;; zend_hash_move_forward_ex(myht, &pos)) {
262: i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
263: if (i == HASH_KEY_NON_EXISTANT)
264: break;
265:
266: if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS) {
267: tmp_ht = HASH_OF(*data);
268: if (tmp_ht) {
269: tmp_ht->nApplyCount++;
270: }
271:
272: if (r == PHP_JSON_OUTPUT_ARRAY) {
273: if (need_comma) {
274: smart_str_appendc(buf, ',');
1.1.1.2 ! misho 275: json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
1.1 misho 276: } else {
277: need_comma = 1;
278: }
1.1.1.2 ! misho 279:
! 280: json_pretty_print_indent(buf, options TSRMLS_CC);
1.1 misho 281: php_json_encode(buf, *data, options TSRMLS_CC);
282: } else if (r == PHP_JSON_OUTPUT_OBJECT) {
283: if (i == HASH_KEY_IS_STRING) {
284: if (key[0] == '\0' && Z_TYPE_PP(val) == IS_OBJECT) {
285: /* Skip protected and private members. */
286: if (tmp_ht) {
287: tmp_ht->nApplyCount--;
288: }
289: continue;
290: }
291:
292: if (need_comma) {
293: smart_str_appendc(buf, ',');
1.1.1.2 ! misho 294: json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
1.1 misho 295: } else {
296: need_comma = 1;
297: }
298:
1.1.1.2 ! misho 299: json_pretty_print_indent(buf, options TSRMLS_CC);
! 300:
1.1 misho 301: json_escape_string(buf, key, key_len - 1, options & ~PHP_JSON_NUMERIC_CHECK TSRMLS_CC);
302: smart_str_appendc(buf, ':');
303:
1.1.1.2 ! misho 304: json_pretty_print_char(buf, options, ' ' TSRMLS_CC);
! 305:
1.1 misho 306: php_json_encode(buf, *data, options TSRMLS_CC);
307: } else {
308: if (need_comma) {
309: smart_str_appendc(buf, ',');
1.1.1.2 ! misho 310: json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
1.1 misho 311: } else {
312: need_comma = 1;
313: }
314:
1.1.1.2 ! misho 315: json_pretty_print_indent(buf, options TSRMLS_CC);
! 316:
1.1 misho 317: smart_str_appendc(buf, '"');
318: smart_str_append_long(buf, (long) index);
319: smart_str_appendc(buf, '"');
320: smart_str_appendc(buf, ':');
321:
1.1.1.2 ! misho 322: json_pretty_print_char(buf, options, ' ' TSRMLS_CC);
! 323:
1.1 misho 324: php_json_encode(buf, *data, options TSRMLS_CC);
325: }
326: }
327:
328: if (tmp_ht) {
329: tmp_ht->nApplyCount--;
330: }
331: }
332: }
333: }
1.1.1.2 ! misho 334:
! 335: --JSON_G(encoder_depth);
! 336: json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
! 337: json_pretty_print_indent(buf, options TSRMLS_CC);
1.1 misho 338:
339: if (r == PHP_JSON_OUTPUT_ARRAY) {
340: smart_str_appendc(buf, ']');
341: } else {
342: smart_str_appendc(buf, '}');
343: }
344: }
345: /* }}} */
346:
347: #define REVERSE16(us) (((us & 0xf) << 12) | (((us >> 4) & 0xf) << 8) | (((us >> 8) & 0xf) << 4) | ((us >> 12) & 0xf))
348:
349: static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */
350: {
1.1.1.2 ! misho 351: int pos = 0, ulen = 0;
1.1 misho 352: unsigned short us;
353: unsigned short *utf16;
354:
355: if (len == 0) {
356: smart_str_appendl(buf, "\"\"", 2);
357: return;
358: }
359:
360: if (options & PHP_JSON_NUMERIC_CHECK) {
361: double d;
362: int type;
363: long p;
364:
365: if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) {
366: if (type == IS_LONG) {
367: smart_str_append_long(buf, p);
368: } else if (type == IS_DOUBLE) {
369: if (!zend_isinf(d) && !zend_isnan(d)) {
370: char *tmp;
371: int l = spprintf(&tmp, 0, "%.*k", (int) EG(precision), d);
372: smart_str_appendl(buf, tmp, l);
373: efree(tmp);
374: } else {
375: php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", d);
376: smart_str_appendc(buf, '0');
377: }
378: }
379: return;
380: }
381:
382: }
1.1.1.2 ! misho 383:
! 384: utf16 = (options & PHP_JSON_UNESCAPED_UNICODE) ? NULL : (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0);
! 385: ulen = utf8_to_utf16(utf16, s, len);
! 386: if (ulen <= 0) {
1.1 misho 387: if (utf16) {
388: efree(utf16);
389: }
1.1.1.2 ! misho 390: if (ulen < 0) {
1.1 misho 391: JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
392: if (!PG(display_errors)) {
393: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
394: }
395: smart_str_appendl(buf, "null", 4);
396: } else {
397: smart_str_appendl(buf, "\"\"", 2);
398: }
399: return;
400: }
1.1.1.2 ! misho 401: if (!(options & PHP_JSON_UNESCAPED_UNICODE)) {
! 402: len = ulen;
! 403: }
1.1 misho 404:
405: smart_str_appendc(buf, '"');
406:
407: while (pos < len)
408: {
1.1.1.2 ! misho 409: us = (options & PHP_JSON_UNESCAPED_UNICODE) ? s[pos++] : utf16[pos++];
1.1 misho 410:
411: switch (us)
412: {
413: case '"':
414: if (options & PHP_JSON_HEX_QUOT) {
415: smart_str_appendl(buf, "\\u0022", 6);
416: } else {
417: smart_str_appendl(buf, "\\\"", 2);
418: }
419: break;
420:
421: case '\\':
422: smart_str_appendl(buf, "\\\\", 2);
423: break;
424:
425: case '/':
1.1.1.2 ! misho 426: if (options & PHP_JSON_UNESCAPED_SLASHES) {
! 427: smart_str_appendc(buf, '/');
! 428: } else {
! 429: smart_str_appendl(buf, "\\/", 2);
! 430: }
1.1 misho 431: break;
432:
433: case '\b':
434: smart_str_appendl(buf, "\\b", 2);
435: break;
436:
437: case '\f':
438: smart_str_appendl(buf, "\\f", 2);
439: break;
440:
441: case '\n':
442: smart_str_appendl(buf, "\\n", 2);
443: break;
444:
445: case '\r':
446: smart_str_appendl(buf, "\\r", 2);
447: break;
448:
449: case '\t':
450: smart_str_appendl(buf, "\\t", 2);
451: break;
452:
453: case '<':
454: if (options & PHP_JSON_HEX_TAG) {
455: smart_str_appendl(buf, "\\u003C", 6);
456: } else {
457: smart_str_appendc(buf, '<');
458: }
459: break;
460:
461: case '>':
462: if (options & PHP_JSON_HEX_TAG) {
463: smart_str_appendl(buf, "\\u003E", 6);
464: } else {
465: smart_str_appendc(buf, '>');
466: }
467: break;
468:
469: case '&':
470: if (options & PHP_JSON_HEX_AMP) {
471: smart_str_appendl(buf, "\\u0026", 6);
472: } else {
473: smart_str_appendc(buf, '&');
474: }
475: break;
476:
477: case '\'':
478: if (options & PHP_JSON_HEX_APOS) {
479: smart_str_appendl(buf, "\\u0027", 6);
480: } else {
481: smart_str_appendc(buf, '\'');
482: }
483: break;
484:
485: default:
1.1.1.2 ! misho 486: if (us >= ' ' && ((options & PHP_JSON_UNESCAPED_UNICODE) || (us & 127) == us)) {
1.1 misho 487: smart_str_appendc(buf, (unsigned char) us);
488: } else {
489: smart_str_appendl(buf, "\\u", 2);
490: us = REVERSE16(us);
491:
492: smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
493: us >>= 4;
494: smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
495: us >>= 4;
496: smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
497: us >>= 4;
498: smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
499: }
500: break;
501: }
502: }
503:
504: smart_str_appendc(buf, '"');
1.1.1.2 ! misho 505: if (utf16) {
! 506: efree(utf16);
! 507: }
! 508: }
! 509: /* }}} */
! 510:
! 511:
! 512: static void json_encode_serializable_object(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */
! 513: {
! 514: zend_class_entry *ce = Z_OBJCE_P(val);
! 515: zval *retval = NULL, fname;
! 516:
! 517: ZVAL_STRING(&fname, "jsonSerialize", 0);
! 518:
! 519: if (FAILURE == call_user_function_ex(EG(function_table), &val, &fname, &retval, 0, NULL, 1, NULL TSRMLS_CC) || !retval) {
! 520: zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed calling %s::jsonSerialize()", ce->name);
! 521: smart_str_appendl(buf, "null", sizeof("null") - 1);
! 522: return;
! 523: }
! 524:
! 525: if (EG(exception)) {
! 526: /* Error already raised */
! 527: zval_ptr_dtor(&retval);
! 528: smart_str_appendl(buf, "null", sizeof("null") - 1);
! 529: return;
! 530: }
! 531:
! 532: if ((Z_TYPE_P(retval) == IS_OBJECT) &&
! 533: (Z_OBJ_HANDLE_P(retval) == Z_OBJ_HANDLE_P(val))) {
! 534: /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */
! 535: json_encode_array(buf, &retval, options TSRMLS_CC);
! 536: } else {
! 537: /* All other types, encode as normal */
! 538: php_json_encode(buf, retval, options TSRMLS_CC);
! 539: }
! 540:
! 541: zval_ptr_dtor(&retval);
1.1 misho 542: }
543: /* }}} */
544:
545: PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */
546: {
547: switch (Z_TYPE_P(val))
548: {
549: case IS_NULL:
550: smart_str_appendl(buf, "null", 4);
551: break;
552:
553: case IS_BOOL:
554: if (Z_BVAL_P(val)) {
555: smart_str_appendl(buf, "true", 4);
556: } else {
557: smart_str_appendl(buf, "false", 5);
558: }
559: break;
560:
561: case IS_LONG:
562: smart_str_append_long(buf, Z_LVAL_P(val));
563: break;
564:
565: case IS_DOUBLE:
566: {
567: char *d = NULL;
568: int len;
569: double dbl = Z_DVAL_P(val);
570:
571: if (!zend_isinf(dbl) && !zend_isnan(dbl)) {
572: len = spprintf(&d, 0, "%.*k", (int) EG(precision), dbl);
573: smart_str_appendl(buf, d, len);
574: efree(d);
575: } else {
576: php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", dbl);
577: smart_str_appendc(buf, '0');
578: }
579: }
580: break;
581:
582: case IS_STRING:
583: json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC);
584: break;
585:
586: case IS_OBJECT:
1.1.1.2 ! misho 587: if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce TSRMLS_CC)) {
! 588: json_encode_serializable_object(buf, val, options TSRMLS_CC);
! 589: break;
! 590: }
! 591: /* fallthrough -- Non-serializable object */
! 592: case IS_ARRAY:
1.1 misho 593: json_encode_array(buf, &val, options TSRMLS_CC);
594: break;
595:
596: default:
597: php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported, encoded as null");
598: smart_str_appendl(buf, "null", 4);
599: break;
600: }
601:
602: return;
603: }
604: /* }}} */
605:
1.1.1.2 ! misho 606: PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, int str_len, int options, long depth TSRMLS_DC) /* {{{ */
1.1 misho 607: {
608: int utf16_len;
609: zval *z;
610: unsigned short *utf16;
611: JSON_parser jp;
612:
613: utf16 = (unsigned short *) safe_emalloc((str_len+1), sizeof(unsigned short), 1);
614:
615: utf16_len = utf8_to_utf16(utf16, str, str_len);
616: if (utf16_len <= 0) {
617: if (utf16) {
618: efree(utf16);
619: }
620: JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
621: RETURN_NULL();
622: }
623:
624: if (depth <= 0) {
625: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Depth must be greater than zero");
626: efree(utf16);
627: RETURN_NULL();
628: }
629:
630: ALLOC_INIT_ZVAL(z);
631: jp = new_JSON_parser(depth);
1.1.1.2 ! misho 632: if (parse_JSON_ex(jp, z, utf16, utf16_len, options TSRMLS_CC)) {
1.1 misho 633: *return_value = *z;
634: }
635: else
636: {
637: double d;
638: int type;
639: long p;
640:
641: RETVAL_NULL();
642: if (str_len == 4) {
643: if (!strcasecmp(str, "null")) {
644: /* We need to explicitly clear the error because its an actual NULL and not an error */
645: jp->error_code = PHP_JSON_ERROR_NONE;
646: RETVAL_NULL();
647: } else if (!strcasecmp(str, "true")) {
648: RETVAL_BOOL(1);
649: }
650: } else if (str_len == 5 && !strcasecmp(str, "false")) {
651: RETVAL_BOOL(0);
652: }
653:
654: if ((type = is_numeric_string(str, str_len, &p, &d, 0)) != 0) {
655: if (type == IS_LONG) {
656: RETVAL_LONG(p);
657: } else if (type == IS_DOUBLE) {
658: RETVAL_DOUBLE(d);
659: }
660: }
661:
662: if (Z_TYPE_P(return_value) != IS_NULL) {
663: jp->error_code = PHP_JSON_ERROR_NONE;
664: }
665:
666: zval_dtor(z);
667: }
668: FREE_ZVAL(z);
669: efree(utf16);
670: JSON_G(error_code) = jp->error_code;
671: free_JSON_parser(jp);
672: }
673: /* }}} */
674:
1.1.1.2 ! misho 675:
1.1 misho 676: /* {{{ proto string json_encode(mixed data [, int options])
677: Returns the JSON representation of a value */
678: static PHP_FUNCTION(json_encode)
679: {
680: zval *parameter;
681: smart_str buf = {0};
682: long options = 0;
683:
684: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", ¶meter, &options) == FAILURE) {
685: return;
686: }
687:
688: JSON_G(error_code) = PHP_JSON_ERROR_NONE;
689:
690: php_json_encode(&buf, parameter, options TSRMLS_CC);
691:
692: ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
693:
694: smart_str_free(&buf);
695: }
696: /* }}} */
697:
698: /* {{{ proto mixed json_decode(string json [, bool assoc [, long depth]])
699: Decodes the JSON representation into a PHP value */
700: static PHP_FUNCTION(json_decode)
701: {
702: char *str;
703: int str_len;
704: zend_bool assoc = 0; /* return JS objects as PHP objects by default */
705: long depth = JSON_PARSER_DEFAULT_DEPTH;
1.1.1.2 ! misho 706: long options = 0;
1.1 misho 707:
1.1.1.2 ! misho 708: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &str, &str_len, &assoc, &depth, &options) == FAILURE) {
1.1 misho 709: return;
710: }
711:
712: JSON_G(error_code) = 0;
713:
714: if (!str_len) {
715: RETURN_NULL();
716: }
717:
1.1.1.2 ! misho 718: /* For BC reasons, the bool $assoc overrides the long $options bit for PHP_JSON_OBJECT_AS_ARRAY */
! 719: if (assoc) {
! 720: options |= PHP_JSON_OBJECT_AS_ARRAY;
! 721: } else {
! 722: options &= ~PHP_JSON_OBJECT_AS_ARRAY;
! 723: }
! 724:
! 725: php_json_decode_ex(return_value, str, str_len, options, depth TSRMLS_CC);
1.1 misho 726: }
727: /* }}} */
728:
729: /* {{{ proto int json_last_error()
730: Returns the error code of the last json_decode(). */
731: static PHP_FUNCTION(json_last_error)
732: {
733: if (zend_parse_parameters_none() == FAILURE) {
734: return;
735: }
736:
737: RETURN_LONG(JSON_G(error_code));
738: }
739: /* }}} */
740:
741: /*
742: * Local variables:
743: * tab-width: 4
744: * c-basic-offset: 4
745: * End:
746: * vim600: noet sw=4 ts=4 fdm=marker
747: * vim<600: noet sw=4 ts=4
748: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>