Home Backend Development PHP Tutorial php json_encode application analysis

php json_encode application analysis

Jul 25, 2016 am 09:04 AM

  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] == '
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles