php json_encode application analysis

WBOY
Release: 2016-07-25 09:04:22
Original
1039 people have browsed it
  1. static PHP_FUNCTION(json_encode)
  2. {
  3. zval *parameter;
  4. smart_str buf = {0};
  5. long options = 0;
  6. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", ¶meter, &options) == FAILURE) {
  7. return;
  8. }
  9. JSON_G(error_code) = PHP_JSON_ERROR_NONE;
  10. php_json_encode(&buf, parameter, options TSRMLS_CC);
  11. ZVAL_STRINGL(return_value, buf.c, buf. len, 1);
  12. smart_str_free(&buf);
  13. }
Copy code

JSON_G(error_code) = PHP_JSON_ERROR_NONE; It is a defined json error. This error can be obtained through the json_last_error function. Have you used it? I haven't used it anyway. php_json_encode is the main operation

  1. PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */
  2. {
  3. switch (Z_TYPE_P(val))
  4. {
  5. case IS_NULL:
  6. smart_str_append l(buf, "null", 4); //Output NULL
  7. break;
  8. case IS_BOOL:
  9. if (Z_BVAL_P(val)) {
  10. smart_str_appendl(buf, "true", 4); //Output true
  11. } else {
  12. smart_str_appendl (buf, "false", 5);//Output false
  13. }
  14. break;
  15. case IS_LONG:
  16. smart_str_append_long(buf, Z_LVAL_P(val));//Output the value of long integer
  17. break;
  18. case IS_DOUBLE:
  19. {
  20. char *d = NULL;
  21. int len;
  22. double dbl = Z_DVAL_P(val);
  23. if (!zend_isinf(dbl) && !zend_isnan(dbl)) {//Non-infinite
  24. len = spprintf(&d , 0, "%.*k", (int) EG(precision), dbl);
  25. smart_str_appendl(buf, d, len);
  26. efree(d);
  27. } else {
  28. php_error_docref(NULL TSRMLS_CC, E_WARNING, " double %.9g does not conform to the JSON spec, encoded as 0", dbl);
  29. smart_str_appendc(buf, '0');
  30. }
  31. }
  32. break;
  33. case IS_STRING://string
  34. json_escape_string(buf , Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC);
  35. break;
  36. case IS_ARRAY://Arrays and objects
  37. case IS_OBJECT:
  38. json_encode_array(buf, &val, options TSRMLS_CC);
  39. break;
  40. default:
  41. php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported, encoded as null");
  42. smart_str_appendl(buf, "null", 4);
  43. break;
  44. }
  45. return;
  46. }
Copy code

Obviously, there will be corresponding cases according to different types. The most complex types are string, array, and object. Arrays and objects are the same operation. Let’s take a look at the string first. It’s very long and the comments are written directly in the code.

  1. //options should be supported only after version 5.3, a binary mask composed of the following constants: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_ OBJECT, JSON_UNESCAPED_UNICODE. Although I have not used it. . .
  2. static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */
  3. {
  4. int pos = 0;
  5. unsigned short us;
  6. unsigned short *utf16;
  7. if ( len == 0) {//If the length is 0, directly return double quotes ""
  8. smart_str_appendl(buf, """", 2);
  9. return;
  10. }
  11. if (options & PHP_JSON_NUMERIC_CHECK) {//Detection Is it a number from 0 to 9? If it is a number, the data will be returned directly as a long or double type.
  12. double d;
  13. int type;
  14. long p;
  15. if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) {
  16. if (type == IS_LONG) {
  17. smart_str_append_long(buf , p);
  18. } else if (type == IS_DOUBLE) {
  19. if (!zend_isinf(d) && !zend_isnan(d)) {
  20. char *tmp;
  21. int l = spprintf(&tmp, 0, "%.* k", (int) EG(precision), d);
  22. smart_str_appendl(buf, tmp, l);
  23. efree(tmp);
  24. } else {
  25. php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", d);
  26. smart_str_appendc(buf, '0');
  27. }
  28. }
  29. return;
  30. }
  31. }
  32. utf16 = (unsigned short *) safe_emalloc(len, sizeof( unsigned short), 0);
  33. len = utf8_to_utf16(utf16, s, len); //The value you input will be processed once and converted into the corresponding Dec code. For example, 1 is 49 and a is 97. Save it to in utf16.
  34. if (len <= 0) {//If len is less than 0, an error has occurred. If you use json_encode to process GBK encoding, it will hang up here.
  35. if (utf16) {
  36. efree(utf16);
  37. }
  38. if (len < 0) {
  39. JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
  40. if (!PG(display_errors)) {
  41. php_error_docref(NULL TSRMLS_CC, E_WARNING, " Invalid UTF-8 sequence in argument");
  42. }
  43. smart_str_appendl(buf, "null", 4);
  44. } else {
  45. smart_str_appendl(buf, """", 2);
  46. }
  47. return;
  48. }
  49. smart_str_appendc(buf, '"'); //Enter "
  50. //The following code is to escape some special characters such as double quotes, backslashes, etc.
  51. while (pos < len)
  52. {
  53. us = utf16[pos++];
  54. switch (us)
  55. {
  56. case '"':
  57. if (options & PHP_JSON_HEX_QUOT) {
  58. smart_str_appendl(buf, "\u0022", 6);
  59. } else {
  60. smart_str_appendl(buf, " \"", 2);
  61. }
  62. break;
  63. case '\':
  64. smart_str_appendl(buf, "\\", 2);
  65. break;
  66. case '/':
  67. smart_str_appendl(buf, "\/" , 2);
  68. break;
  69. case 'b':
  70. smart_str_appendl(buf, "\b", 2);
  71. break;
  72. case 'f':
  73. smart_str_appendl(buf, "\f", 2);
  74. break;
  75. case 'n':
  76. smart_str_appendl(buf, "\n", 2);
  77. break;
  78. case 'r':
  79. smart_str_appendl(buf, "\r", 2);
  80. break;
  81. case 't':
  82. smart_str_appendl(buf, "\t", 2);
  83. break;
  84. case '<':
  85. if (options & PHP_JSON_HEX_TAG) {
  86. smart_str_appendl(buf, "\u003C", 6) ;
  87. } else {
  88. smart_str_appendc(buf, '<');
  89. }
  90. break;
  91. case '>':
  92. if (options & PHP_JSON_HEX_TAG) {
  93. smart_str_appendl(buf, "\u003E", 6);
  94. } else {
  95. smart_str_appendc(buf, '>');
  96. }
  97. break;
  98. case '&':
  99. if (options & PHP_JSON_HEX_AMP) {
  100. smart_str_appendl(buf, "\u0026", 6);
  101. } else {
  102. smart_str_appendc(buf, '&');
  103. }
  104. break;
  105. case ''':
  106. if (options & PHP_JSON_HEX_APOS) {
  107. smart_str_appendl(buf, "\u0027", 6);
  108. } else {
  109. smart_str_appendc(buf, ''');
  110. }
  111. break;
  112. default: //Up to here, the value will be appended to buf without special characters
  113. if (us >= ' ' && (us & 127) == us) {
  114. smart_str_appendc(buf, (unsigned char) us);
  115. }else {
  116. smart_str_appendl(buf, "\u", 2);
  117. us = REVERSE16(us);
  118. smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
  119. us >>= 4;
  120. smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
  121. us >>= 4;
  122. smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
  123. us >>= 4;
  124. smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
  125. }
  126. break;
  127. }
  128. }
  129. smart_str_appendc(buf, '"'); //结束 双引号。
  130. efree(utf16);
  131. }
复制代码

Let’s take a look at arrays and objects. It’s also very simple.

  1. static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) /* {{{ */
  2. {
  3. int i, r;
  4. HashTable *myht;
  5. if (Z_TYPE_PP(val) == IS_ARRAY) {
  6. myht = HASH_OF(*val);
  7. r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val TSRMLS_CC);
  8. } else {
  9. myht = Z_OBJPROP_PP(val);
  10. r = PHP_JSON_OUTPUT_OBJECT;
  11. }
  12. if (myht && myht->nApplyCount > 1) {
  13. php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
  14. smart_str_appendl(buf, "null", 4);
  15. return;
  16. }
  17. //开始标签
  18. if (r == PHP_JSON_OUTPUT_ARRAY) {
  19. smart_str_appendc(buf, '[');
  20. } else {
  21. smart_str_appendc(buf, '{');
  22. }
  23. i = myht ? zend_hash_num_elements(myht) : 0;
  24. if (i > 0)
  25. {
  26. char *key;
  27. zval **data;
  28. ulong index;
  29. uint key_len;
  30. HashPosition pos;
  31. HashTable *tmp_ht;
  32. int need_comma = 0;
  33. zend_hash_internal_pointer_reset_ex(myht, &pos);
  34. //便利哈希表
  35. for (;; zend_hash_move_forward_ex(myht, &pos)) {
  36. i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
  37. if (i == HASH_KEY_NON_EXISTANT)
  38. break;
  39. if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS) {
  40. tmp_ht = HASH_OF(*data);
  41. if (tmp_ht) {
  42. tmp_ht->nApplyCount++;
  43. }
  44. if (r == PHP_JSON_OUTPUT_ARRAY) {
  45. if (need_comma) {
  46. smart_str_appendc(buf, ',');
  47. } else {
  48. need_comma = 1;
  49. }
  50. //将值append到 buf中
  51. php_json_encode(buf, *data, options TSRMLS_CC);
  52. } else if (r == PHP_JSON_OUTPUT_OBJECT) {
  53. if (i == HASH_KEY_IS_STRING) {
  54. if (key[0] == '
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template