php json_encode应用分析

WBOY
Libérer: 2016-07-25 09:04:22
original
955 Les gens l'ont consulté
  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. }
复制代码

JSON_G(error_code) = PHP_JSON_ERROR_NONE; 是定义的json错误,该错误可以通过json_last_error函数获取,你用过吗?反正我没用过。 php_json_encode是主要的操作

  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_appendl(buf, "null", 4); //输出NULL
  7. break;
  8. case IS_BOOL:
  9. if (Z_BVAL_P(val)) {
  10. smart_str_appendl(buf, "true", 4);//输出true
  11. } else {
  12. smart_str_appendl(buf, "false", 5);//输出false
  13. }
  14. break;
  15. case IS_LONG:
  16. smart_str_append_long(buf, Z_LVAL_P(val));//输出长整形的值
  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)) {//非无穷尽
  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://字符串
  34. json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC);
  35. break;
  36. case IS_ARRAY://数组和对象
  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. }
复制代码

很明显,根据不同的类型,会有相应的case。 最复杂的是 字符串 、数组 、对象这三种类型,数组和对象是同一种操作。 先看看字符串吧,很长,注释直接写在代码里。

  1. //options应该是5.3版本之后才支持的,由以下常量组成的二进制掩码: 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.虽然我没用过。。。
  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) {//如果长度为0,则直接返回 双引号 ""
  8. smart_str_appendl(buf, "\"\"", 2);
  9. return;
  10. }
  11. if (options & PHP_JSON_NUMERIC_CHECK) {//检测是否为0-9的数字,如果是数字,那么就会直接把数据作为long或double类型返回。
  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); //这里会对你输入的值一次处理转成对应的Dec码,比如1是49,a是97这样的,保存到utf16中。
  34. if (len if (utf16) {
  35. efree(utf16);
  36. }
  37. if (len JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
  38. if (!PG(display_errors)) {
  39. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
  40. }
  41. smart_str_appendl(buf, "null", 4);
  42. } else {
  43. smart_str_appendl(buf, "\"\"", 2);
  44. }
  45. return;
  46. }
  47. smart_str_appendc(buf, '"'); //输入 \"
  48. //下面这一段代码就是将一些特殊字符转义如 双引号,反斜线等等
  49. while (pos {
  50. us = utf16[pos++];
  51. switch (us)
  52. {
  53. case '"':
  54. if (options & PHP_JSON_HEX_QUOT) {
  55. smart_str_appendl(buf, "\\u0022", 6);
  56. } else {
  57. smart_str_appendl(buf, "\\\"", 2);
  58. }
  59. break;
  60. case '\\':
  61. smart_str_appendl(buf, "\\\\", 2);
  62. break;
  63. case '/':
  64. smart_str_appendl(buf, "\\/", 2);
  65. break;
  66. case '\b':
  67. smart_str_appendl(buf, "\\b", 2);
  68. break;
  69. case '\f':
  70. smart_str_appendl(buf, "\\f", 2);
  71. break;
  72. case '\n':
  73. smart_str_appendl(buf, "\\n", 2);
  74. break;
  75. case '\r':
  76. smart_str_appendl(buf, "\\r", 2);
  77. break;
  78. case '\t':
  79. smart_str_appendl(buf, "\\t", 2);
  80. break;
  81. case ' if (options & PHP_JSON_HEX_TAG) {
  82. smart_str_appendl(buf, "\\u003C", 6);
  83. } else {
  84. smart_str_appendc(buf, ' }
  85. break;
  86. case '>':
  87. if (options & PHP_JSON_HEX_TAG) {
  88. smart_str_appendl(buf, "\\u003E", 6);
  89. } else {
  90. smart_str_appendc(buf, '>');
  91. }
  92. break;
  93. case '&':
  94. if (options & PHP_JSON_HEX_AMP) {
  95. smart_str_appendl(buf, "\\u0026", 6);
  96. } else {
  97. smart_str_appendc(buf, '&');
  98. }
  99. break;
  100. case '\'':
  101. if (options & PHP_JSON_HEX_APOS) {
  102. smart_str_appendl(buf, "\\u0027", 6);
  103. } else {
  104. smart_str_appendc(buf, '\'');
  105. }
  106. break;
  107. default: //一直到这里,没有特殊字符就会把值append到buf中
  108. if (us >= ' ' && (us & 127) == us) {
  109. smart_str_appendc(buf, (unsigned char) us);
  110. } else {
  111. smart_str_appendl(buf, "\\u", 2);
  112. us = REVERSE16(us);
  113. smart_str_appendc(buf, digits[us & ((1 us >>= 4;
  114. smart_str_appendc(buf, digits[us & ((1 us >>= 4;
  115. smart_str_appendc(buf, digits[us & ((1 us >>= 4;
  116. smart_str_appendc(buf, digits[us & ((1 }
  117. break;
  118. }
  119. }
  120. smart_str_appendc(buf, '"'); //结束 双引号。
  121. efree(utf16);
  122. }
复制代码

再来看看数组和对象,也很简单,

  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] == '\0' && Z_TYPE_PP(val) == IS_OBJECT) {
  55. /* Skip protected and private members. */
  56. if (tmp_ht) {
  57. tmp_ht->nApplyCount--;
  58. }
  59. continue;
  60. }
  61. if (need_comma) {
  62. smart_str_appendc(buf, ',');
  63. } else {
  64. need_comma = 1;
  65. }
  66. json_escape_string(buf, key, key_len - 1, options & ~PHP_JSON_NUMERIC_CHECK TSRMLS_CC);
  67. smart_str_appendc(buf, ':');
  68. php_json_encode(buf, *data, options TSRMLS_CC);
  69. } else {
  70. if (need_comma) {
  71. smart_str_appendc(buf, ',');
  72. } else {
  73. need_comma = 1;
  74. }
  75. smart_str_appendc(buf, '"');
  76. smart_str_append_long(buf, (long) index);
  77. smart_str_appendc(buf, '"');
  78. smart_str_appendc(buf, ':');
  79. php_json_encode(buf, *data, options TSRMLS_CC);
  80. }
  81. }
  82. if (tmp_ht) {
  83. tmp_ht->nApplyCount--;
  84. }
  85. }
  86. }
  87. }
  88. //结束标签
  89. if (r == PHP_JSON_OUTPUT_ARRAY) {
  90. smart_str_appendc(buf, ']');
  91. } else {
  92. smart_str_appendc(buf, '}');
  93. }
  94. }
复制代码

通过简单分析,证明了一个问题,跟我上面用sprintf的方法其实是一样的,都是拼接字符串,

而且 为了性能,更应该鼓励用sprintf来拼接json格式,

因为 json_encode会进行很多 循环操作,而且所消耗的性能是线性的 O(n)。



Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!