json_encoder.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Omar Kilani <omar@php.net> |
  14. | Jakub Zelenka <bukka@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #include "php_ini.h"
  22. #include "ext/standard/info.h"
  23. #include "ext/standard/html.h"
  24. #include "zend_smart_str.h"
  25. #include "php_json.h"
  26. #include "php_json_encoder.h"
  27. #include <zend_exceptions.h>
  28. #include "zend_enum.h"
  29. static const char digits[] = "0123456789abcdef";
  30. static int php_json_escape_string(
  31. smart_str *buf, const char *s, size_t len,
  32. int options, php_json_encoder *encoder);
  33. static int php_json_determine_array_type(zval *val) /* {{{ */
  34. {
  35. zend_array *myht = Z_ARRVAL_P(val);
  36. if (myht) {
  37. return zend_array_is_list(myht) ? PHP_JSON_OUTPUT_ARRAY : PHP_JSON_OUTPUT_OBJECT;
  38. }
  39. return PHP_JSON_OUTPUT_ARRAY;
  40. }
  41. /* }}} */
  42. /* {{{ Pretty printing support functions */
  43. static inline void php_json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */
  44. {
  45. if (options & PHP_JSON_PRETTY_PRINT) {
  46. smart_str_appendc(buf, c);
  47. }
  48. }
  49. /* }}} */
  50. static inline void php_json_pretty_print_indent(smart_str *buf, int options, php_json_encoder *encoder) /* {{{ */
  51. {
  52. int i;
  53. if (options & PHP_JSON_PRETTY_PRINT) {
  54. for (i = 0; i < encoder->depth; ++i) {
  55. smart_str_appendl(buf, " ", 4);
  56. }
  57. }
  58. }
  59. /* }}} */
  60. /* }}} */
  61. static inline int php_json_is_valid_double(double d) /* {{{ */
  62. {
  63. return !zend_isinf(d) && !zend_isnan(d);
  64. }
  65. /* }}} */
  66. static inline void php_json_encode_double(smart_str *buf, double d, int options) /* {{{ */
  67. {
  68. size_t len;
  69. char num[ZEND_DOUBLE_MAX_LENGTH];
  70. zend_gcvt(d, (int)PG(serialize_precision), '.', 'e', num);
  71. len = strlen(num);
  72. if (options & PHP_JSON_PRESERVE_ZERO_FRACTION && strchr(num, '.') == NULL && len < ZEND_DOUBLE_MAX_LENGTH - 2) {
  73. num[len++] = '.';
  74. num[len++] = '0';
  75. num[len] = '\0';
  76. }
  77. smart_str_appendl(buf, num, len);
  78. }
  79. /* }}} */
  80. #define PHP_JSON_HASH_PROTECT_RECURSION(_tmp_ht) \
  81. do { \
  82. if (_tmp_ht) { \
  83. GC_TRY_PROTECT_RECURSION(_tmp_ht); \
  84. } \
  85. } while (0)
  86. #define PHP_JSON_HASH_UNPROTECT_RECURSION(_tmp_ht) \
  87. do { \
  88. if (_tmp_ht) { \
  89. GC_TRY_UNPROTECT_RECURSION(_tmp_ht); \
  90. } \
  91. } while (0)
  92. static int php_json_encode_array(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
  93. {
  94. int i, r, need_comma = 0;
  95. HashTable *myht, *prop_ht;
  96. if (Z_TYPE_P(val) == IS_ARRAY) {
  97. myht = Z_ARRVAL_P(val);
  98. prop_ht = NULL;
  99. r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : php_json_determine_array_type(val);
  100. } else if (Z_OBJ_P(val)->properties == NULL
  101. && Z_OBJ_HT_P(val)->get_properties_for == NULL
  102. && Z_OBJ_HT_P(val)->get_properties == zend_std_get_properties) {
  103. /* Optimized version without rebuilding properties HashTable */
  104. zend_object *obj = Z_OBJ_P(val);
  105. zend_class_entry *ce = obj->ce;
  106. zend_property_info *prop_info;
  107. zval *prop;
  108. int i;
  109. if (GC_IS_RECURSIVE(obj)) {
  110. encoder->error_code = PHP_JSON_ERROR_RECURSION;
  111. smart_str_appendl(buf, "null", 4);
  112. return FAILURE;
  113. }
  114. PHP_JSON_HASH_PROTECT_RECURSION(obj);
  115. smart_str_appendc(buf, '{');
  116. ++encoder->depth;
  117. for (i = 0; i < ce->default_properties_count; i++) {
  118. prop_info = ce->properties_info_table[i];
  119. if (!prop_info) {
  120. continue;
  121. }
  122. if (ZSTR_VAL(prop_info->name)[0] == '\0' && ZSTR_LEN(prop_info->name) > 0) {
  123. /* Skip protected and private members. */
  124. continue;
  125. }
  126. prop = OBJ_PROP(obj, prop_info->offset);
  127. if (Z_TYPE_P(prop) == IS_UNDEF) {
  128. continue;
  129. }
  130. if (need_comma) {
  131. smart_str_appendc(buf, ',');
  132. } else {
  133. need_comma = 1;
  134. }
  135. php_json_pretty_print_char(buf, options, '\n');
  136. php_json_pretty_print_indent(buf, options, encoder);
  137. if (php_json_escape_string(buf, ZSTR_VAL(prop_info->name), ZSTR_LEN(prop_info->name),
  138. options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE &&
  139. (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) &&
  140. buf->s) {
  141. ZSTR_LEN(buf->s) -= 4;
  142. smart_str_appendl(buf, "\"\"", 2);
  143. }
  144. smart_str_appendc(buf, ':');
  145. php_json_pretty_print_char(buf, options, ' ');
  146. if (php_json_encode_zval(buf, prop, options, encoder) == FAILURE &&
  147. !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
  148. PHP_JSON_HASH_UNPROTECT_RECURSION(obj);
  149. return FAILURE;
  150. }
  151. }
  152. PHP_JSON_HASH_UNPROTECT_RECURSION(obj);
  153. if (encoder->depth > encoder->max_depth) {
  154. encoder->error_code = PHP_JSON_ERROR_DEPTH;
  155. if (!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
  156. return FAILURE;
  157. }
  158. }
  159. --encoder->depth;
  160. if (need_comma) {
  161. php_json_pretty_print_char(buf, options, '\n');
  162. php_json_pretty_print_indent(buf, options, encoder);
  163. }
  164. smart_str_appendc(buf, '}');
  165. return SUCCESS;
  166. } else {
  167. prop_ht = myht = zend_get_properties_for(val, ZEND_PROP_PURPOSE_JSON);
  168. r = PHP_JSON_OUTPUT_OBJECT;
  169. }
  170. if (myht && GC_IS_RECURSIVE(myht)) {
  171. encoder->error_code = PHP_JSON_ERROR_RECURSION;
  172. smart_str_appendl(buf, "null", 4);
  173. zend_release_properties(prop_ht);
  174. return FAILURE;
  175. }
  176. PHP_JSON_HASH_PROTECT_RECURSION(myht);
  177. if (r == PHP_JSON_OUTPUT_ARRAY) {
  178. smart_str_appendc(buf, '[');
  179. } else {
  180. smart_str_appendc(buf, '{');
  181. }
  182. ++encoder->depth;
  183. i = myht ? zend_hash_num_elements(myht) : 0;
  184. if (i > 0) {
  185. zend_string *key;
  186. zval *data;
  187. zend_ulong index;
  188. ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) {
  189. if (r == PHP_JSON_OUTPUT_ARRAY) {
  190. if (need_comma) {
  191. smart_str_appendc(buf, ',');
  192. } else {
  193. need_comma = 1;
  194. }
  195. php_json_pretty_print_char(buf, options, '\n');
  196. php_json_pretty_print_indent(buf, options, encoder);
  197. } else if (r == PHP_JSON_OUTPUT_OBJECT) {
  198. if (key) {
  199. if (ZSTR_VAL(key)[0] == '\0' && ZSTR_LEN(key) > 0 && Z_TYPE_P(val) == IS_OBJECT) {
  200. /* Skip protected and private members. */
  201. continue;
  202. }
  203. if (need_comma) {
  204. smart_str_appendc(buf, ',');
  205. } else {
  206. need_comma = 1;
  207. }
  208. php_json_pretty_print_char(buf, options, '\n');
  209. php_json_pretty_print_indent(buf, options, encoder);
  210. if (php_json_escape_string(buf, ZSTR_VAL(key), ZSTR_LEN(key),
  211. options & ~PHP_JSON_NUMERIC_CHECK, encoder) == FAILURE &&
  212. (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) &&
  213. buf->s) {
  214. ZSTR_LEN(buf->s) -= 4;
  215. smart_str_appendl(buf, "\"\"", 2);
  216. }
  217. } else {
  218. if (need_comma) {
  219. smart_str_appendc(buf, ',');
  220. } else {
  221. need_comma = 1;
  222. }
  223. php_json_pretty_print_char(buf, options, '\n');
  224. php_json_pretty_print_indent(buf, options, encoder);
  225. smart_str_appendc(buf, '"');
  226. smart_str_append_long(buf, (zend_long) index);
  227. smart_str_appendc(buf, '"');
  228. }
  229. smart_str_appendc(buf, ':');
  230. php_json_pretty_print_char(buf, options, ' ');
  231. }
  232. if (php_json_encode_zval(buf, data, options, encoder) == FAILURE &&
  233. !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
  234. PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
  235. zend_release_properties(prop_ht);
  236. return FAILURE;
  237. }
  238. } ZEND_HASH_FOREACH_END();
  239. }
  240. PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
  241. if (encoder->depth > encoder->max_depth) {
  242. encoder->error_code = PHP_JSON_ERROR_DEPTH;
  243. if (!(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
  244. zend_release_properties(prop_ht);
  245. return FAILURE;
  246. }
  247. }
  248. --encoder->depth;
  249. /* Only keep closing bracket on same line for empty arrays/objects */
  250. if (need_comma) {
  251. php_json_pretty_print_char(buf, options, '\n');
  252. php_json_pretty_print_indent(buf, options, encoder);
  253. }
  254. if (r == PHP_JSON_OUTPUT_ARRAY) {
  255. smart_str_appendc(buf, ']');
  256. } else {
  257. smart_str_appendc(buf, '}');
  258. }
  259. zend_release_properties(prop_ht);
  260. return SUCCESS;
  261. }
  262. /* }}} */
  263. static int php_json_escape_string(
  264. smart_str *buf, const char *s, size_t len,
  265. int options, php_json_encoder *encoder) /* {{{ */
  266. {
  267. int status;
  268. unsigned int us;
  269. size_t pos, checkpoint;
  270. char *dst;
  271. if (len == 0) {
  272. smart_str_appendl(buf, "\"\"", 2);
  273. return SUCCESS;
  274. }
  275. if (options & PHP_JSON_NUMERIC_CHECK) {
  276. double d;
  277. int type;
  278. zend_long p;
  279. if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) {
  280. if (type == IS_LONG) {
  281. smart_str_append_long(buf, p);
  282. return SUCCESS;
  283. } else if (type == IS_DOUBLE && php_json_is_valid_double(d)) {
  284. php_json_encode_double(buf, d, options);
  285. return SUCCESS;
  286. }
  287. }
  288. }
  289. checkpoint = buf->s ? ZSTR_LEN(buf->s) : 0;
  290. /* pre-allocate for string length plus 2 quotes */
  291. smart_str_alloc(buf, len+2, 0);
  292. smart_str_appendc(buf, '"');
  293. pos = 0;
  294. do {
  295. static const uint32_t charmap[8] = {
  296. 0xffffffff, 0x500080c4, 0x10000000, 0x00000000,
  297. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff};
  298. us = (unsigned char)s[pos];
  299. if (EXPECTED(!ZEND_BIT_TEST(charmap, us))) {
  300. pos++;
  301. len--;
  302. if (len == 0) {
  303. smart_str_appendl(buf, s, pos);
  304. break;
  305. }
  306. } else {
  307. if (pos) {
  308. smart_str_appendl(buf, s, pos);
  309. s += pos;
  310. pos = 0;
  311. }
  312. us = (unsigned char)s[0];
  313. if (UNEXPECTED(us >= 0x80)) {
  314. us = php_next_utf8_char((unsigned char *)s, len, &pos, &status);
  315. /* check whether UTF8 character is correct */
  316. if (UNEXPECTED(status != SUCCESS)) {
  317. if (options & PHP_JSON_INVALID_UTF8_IGNORE) {
  318. /* ignore invalid UTF8 character */
  319. } else if (options & PHP_JSON_INVALID_UTF8_SUBSTITUTE) {
  320. /* Use Unicode character 'REPLACEMENT CHARACTER' (U+FFFD) */
  321. if (options & PHP_JSON_UNESCAPED_UNICODE) {
  322. smart_str_appendl(buf, "\xef\xbf\xbd", 3);
  323. } else {
  324. smart_str_appendl(buf, "\\ufffd", 6);
  325. }
  326. } else {
  327. ZSTR_LEN(buf->s) = checkpoint;
  328. encoder->error_code = PHP_JSON_ERROR_UTF8;
  329. if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
  330. smart_str_appendl(buf, "null", 4);
  331. }
  332. return FAILURE;
  333. }
  334. /* Escape U+2028/U+2029 line terminators, UNLESS both
  335. JSON_UNESCAPED_UNICODE and
  336. JSON_UNESCAPED_LINE_TERMINATORS were provided */
  337. } else if ((options & PHP_JSON_UNESCAPED_UNICODE)
  338. && ((options & PHP_JSON_UNESCAPED_LINE_TERMINATORS)
  339. || us < 0x2028 || us > 0x2029)) {
  340. smart_str_appendl(buf, s, pos);
  341. } else {
  342. /* From http://en.wikipedia.org/wiki/UTF16 */
  343. if (us >= 0x10000) {
  344. unsigned int next_us;
  345. us -= 0x10000;
  346. next_us = (unsigned short)((us & 0x3ff) | 0xdc00);
  347. us = (unsigned short)((us >> 10) | 0xd800);
  348. dst = smart_str_extend(buf, 6);
  349. dst[0] = '\\';
  350. dst[1] = 'u';
  351. dst[2] = digits[(us >> 12) & 0xf];
  352. dst[3] = digits[(us >> 8) & 0xf];
  353. dst[4] = digits[(us >> 4) & 0xf];
  354. dst[5] = digits[us & 0xf];
  355. us = next_us;
  356. }
  357. dst = smart_str_extend(buf, 6);
  358. dst[0] = '\\';
  359. dst[1] = 'u';
  360. dst[2] = digits[(us >> 12) & 0xf];
  361. dst[3] = digits[(us >> 8) & 0xf];
  362. dst[4] = digits[(us >> 4) & 0xf];
  363. dst[5] = digits[us & 0xf];
  364. }
  365. s += pos;
  366. len -= pos;
  367. pos = 0;
  368. } else {
  369. s++;
  370. switch (us) {
  371. case '"':
  372. if (options & PHP_JSON_HEX_QUOT) {
  373. smart_str_appendl(buf, "\\u0022", 6);
  374. } else {
  375. smart_str_appendl(buf, "\\\"", 2);
  376. }
  377. break;
  378. case '\\':
  379. smart_str_appendl(buf, "\\\\", 2);
  380. break;
  381. case '/':
  382. if (options & PHP_JSON_UNESCAPED_SLASHES) {
  383. smart_str_appendc(buf, '/');
  384. } else {
  385. smart_str_appendl(buf, "\\/", 2);
  386. }
  387. break;
  388. case '\b':
  389. smart_str_appendl(buf, "\\b", 2);
  390. break;
  391. case '\f':
  392. smart_str_appendl(buf, "\\f", 2);
  393. break;
  394. case '\n':
  395. smart_str_appendl(buf, "\\n", 2);
  396. break;
  397. case '\r':
  398. smart_str_appendl(buf, "\\r", 2);
  399. break;
  400. case '\t':
  401. smart_str_appendl(buf, "\\t", 2);
  402. break;
  403. case '<':
  404. if (options & PHP_JSON_HEX_TAG) {
  405. smart_str_appendl(buf, "\\u003C", 6);
  406. } else {
  407. smart_str_appendc(buf, '<');
  408. }
  409. break;
  410. case '>':
  411. if (options & PHP_JSON_HEX_TAG) {
  412. smart_str_appendl(buf, "\\u003E", 6);
  413. } else {
  414. smart_str_appendc(buf, '>');
  415. }
  416. break;
  417. case '&':
  418. if (options & PHP_JSON_HEX_AMP) {
  419. smart_str_appendl(buf, "\\u0026", 6);
  420. } else {
  421. smart_str_appendc(buf, '&');
  422. }
  423. break;
  424. case '\'':
  425. if (options & PHP_JSON_HEX_APOS) {
  426. smart_str_appendl(buf, "\\u0027", 6);
  427. } else {
  428. smart_str_appendc(buf, '\'');
  429. }
  430. break;
  431. default:
  432. ZEND_ASSERT(us < ' ');
  433. dst = smart_str_extend(buf, 6);
  434. dst[0] = '\\';
  435. dst[1] = 'u';
  436. dst[2] = '0';
  437. dst[3] = '0';
  438. dst[4] = digits[(us >> 4) & 0xf];
  439. dst[5] = digits[us & 0xf];
  440. break;
  441. }
  442. len--;
  443. }
  444. }
  445. } while (len);
  446. smart_str_appendc(buf, '"');
  447. return SUCCESS;
  448. }
  449. /* }}} */
  450. static int php_json_encode_serializable_object(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
  451. {
  452. zend_class_entry *ce = Z_OBJCE_P(val);
  453. HashTable* myht = Z_OBJPROP_P(val);
  454. zval retval, fname;
  455. int return_code;
  456. if (myht && GC_IS_RECURSIVE(myht)) {
  457. encoder->error_code = PHP_JSON_ERROR_RECURSION;
  458. if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
  459. smart_str_appendl(buf, "null", 4);
  460. }
  461. return FAILURE;
  462. }
  463. PHP_JSON_HASH_PROTECT_RECURSION(myht);
  464. ZVAL_STRING(&fname, "jsonSerialize");
  465. if (FAILURE == call_user_function(NULL, val, &fname, &retval, 0, NULL) || Z_TYPE(retval) == IS_UNDEF) {
  466. if (!EG(exception)) {
  467. zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ZSTR_VAL(ce->name));
  468. }
  469. zval_ptr_dtor(&fname);
  470. if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
  471. smart_str_appendl(buf, "null", 4);
  472. }
  473. PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
  474. return FAILURE;
  475. }
  476. if (EG(exception)) {
  477. /* Error already raised */
  478. zval_ptr_dtor(&retval);
  479. zval_ptr_dtor(&fname);
  480. if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
  481. smart_str_appendl(buf, "null", 4);
  482. }
  483. PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
  484. return FAILURE;
  485. }
  486. if ((Z_TYPE(retval) == IS_OBJECT) &&
  487. (Z_OBJ(retval) == Z_OBJ_P(val))) {
  488. /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */
  489. PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
  490. return_code = php_json_encode_array(buf, &retval, options, encoder);
  491. } else {
  492. /* All other types, encode as normal */
  493. return_code = php_json_encode_zval(buf, &retval, options, encoder);
  494. PHP_JSON_HASH_UNPROTECT_RECURSION(myht);
  495. }
  496. zval_ptr_dtor(&retval);
  497. zval_ptr_dtor(&fname);
  498. return return_code;
  499. }
  500. /* }}} */
  501. static int php_json_encode_serializable_enum(smart_str *buf, zval *val, int options, php_json_encoder *encoder)
  502. {
  503. zend_class_entry *ce = Z_OBJCE_P(val);
  504. if (ce->enum_backing_type == IS_UNDEF) {
  505. encoder->error_code = PHP_JSON_ERROR_NON_BACKED_ENUM;
  506. smart_str_appendc(buf, '0');
  507. return FAILURE;
  508. }
  509. zval *value_zv = zend_enum_fetch_case_value(Z_OBJ_P(val));
  510. return php_json_encode_zval(buf, value_zv, options, encoder);
  511. }
  512. int php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
  513. {
  514. again:
  515. switch (Z_TYPE_P(val))
  516. {
  517. case IS_NULL:
  518. smart_str_appendl(buf, "null", 4);
  519. break;
  520. case IS_TRUE:
  521. smart_str_appendl(buf, "true", 4);
  522. break;
  523. case IS_FALSE:
  524. smart_str_appendl(buf, "false", 5);
  525. break;
  526. case IS_LONG:
  527. smart_str_append_long(buf, Z_LVAL_P(val));
  528. break;
  529. case IS_DOUBLE:
  530. if (php_json_is_valid_double(Z_DVAL_P(val))) {
  531. php_json_encode_double(buf, Z_DVAL_P(val), options);
  532. } else {
  533. encoder->error_code = PHP_JSON_ERROR_INF_OR_NAN;
  534. smart_str_appendc(buf, '0');
  535. }
  536. break;
  537. case IS_STRING:
  538. return php_json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options, encoder);
  539. case IS_OBJECT:
  540. if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) {
  541. return php_json_encode_serializable_object(buf, val, options, encoder);
  542. }
  543. if (Z_OBJCE_P(val)->ce_flags & ZEND_ACC_ENUM) {
  544. return php_json_encode_serializable_enum(buf, val, options, encoder);
  545. }
  546. /* fallthrough -- Non-serializable object */
  547. ZEND_FALLTHROUGH;
  548. case IS_ARRAY: {
  549. /* Avoid modifications (and potential freeing) of the array through a reference when a
  550. * jsonSerialize() method is invoked. */
  551. zval zv;
  552. int res;
  553. ZVAL_COPY(&zv, val);
  554. res = php_json_encode_array(buf, &zv, options, encoder);
  555. zval_ptr_dtor_nogc(&zv);
  556. return res;
  557. }
  558. case IS_REFERENCE:
  559. val = Z_REFVAL_P(val);
  560. goto again;
  561. default:
  562. encoder->error_code = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
  563. if (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
  564. smart_str_appendl(buf, "null", 4);
  565. }
  566. return FAILURE;
  567. }
  568. return SUCCESS;
  569. }
  570. /* }}} */