-
- static PHP_FUNCTION(json_encode)
- {
- zval *parameter;
- smart_str buf = {0};
- long options = 0;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", ¶meter, &options) == FAILURE) {
- return;
- }
-
- JSON_G(error_code) = PHP_JSON_ERROR_NONE;
-
- php_json_encode(&buf, parameter, options TSRMLS_CC);
-
- ZVAL_STRINGL(return_value, buf.c, buf. len, 1);
-
- smart_str_free(&buf);
- }
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
-
- PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */
- {
- switch (Z_TYPE_P(val))
- {
- case IS_NULL:
- smart_str_append l(buf, "null", 4); //Output NULL
- break;
-
- case IS_BOOL:
- if (Z_BVAL_P(val)) {
- smart_str_appendl(buf, "true", 4); //Output true
- } else {
- smart_str_appendl (buf, "false", 5);//Output false
- }
- break;
-
- case IS_LONG:
- smart_str_append_long(buf, Z_LVAL_P(val));//Output the value of long integer
- break;
-
- case IS_DOUBLE:
- {
- char *d = NULL;
- int len;
- double dbl = Z_DVAL_P(val);
-
- if (!zend_isinf(dbl) && !zend_isnan(dbl)) {//Non-infinite
- len = spprintf(&d , 0, "%.*k", (int) EG(precision), dbl);
- smart_str_appendl(buf, d, len);
- efree(d);
- } else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, " double %.9g does not conform to the JSON spec, encoded as 0", dbl);
- smart_str_appendc(buf, '0');
- }
- }
- break;
-
- case IS_STRING://string
- json_escape_string(buf , Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC);
- break;
-
- case IS_ARRAY://Arrays and objects
- case IS_OBJECT:
- json_encode_array(buf, &val, options TSRMLS_CC);
- break;
-
- default:
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported, encoded as null");
- smart_str_appendl(buf, "null", 4);
- break;
- }
-
- return;
- }
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.
-
- //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. . .
- static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */
- {
- int pos = 0;
- unsigned short us;
- unsigned short *utf16;
-
- if ( len == 0) {//If the length is 0, directly return double quotes ""
- smart_str_appendl(buf, """", 2);
- return;
- }
-
- 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.
- double d;
- int type;
- long p;
-
- if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) {
- if (type == IS_LONG) {
- smart_str_append_long(buf , p);
- } else if (type == IS_DOUBLE) {
- if (!zend_isinf(d) && !zend_isnan(d)) {
- char *tmp;
- int l = spprintf(&tmp, 0, "%.* k", (int) EG(precision), d);
- smart_str_appendl(buf, tmp, l);
- efree(tmp);
- } else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", d);
- smart_str_appendc(buf, '0');
- }
- }
- return;
- }
-
- }
-
- utf16 = (unsigned short *) safe_emalloc(len, sizeof( unsigned short), 0);
- 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.
- 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.
- if (utf16) {
- efree(utf16);
- }
- if (len < 0) {
- JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
- if (!PG(display_errors)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, " Invalid UTF-8 sequence in argument");
- }
- smart_str_appendl(buf, "null", 4);
- } else {
- smart_str_appendl(buf, """", 2);
- }
- return;
- }
-
- smart_str_appendc(buf, '"'); //Enter "
-
- //The following code is to escape some special characters such as double quotes, backslashes, etc.
- while (pos < len)
- {
- us = utf16[pos++];
-
- switch (us)
- {
- case '"':
- if (options & PHP_JSON_HEX_QUOT) {
- smart_str_appendl(buf, "\u0022", 6);
- } else {
- smart_str_appendl(buf, " \"", 2);
- }
- break;
-
- case '\':
- smart_str_appendl(buf, "\\", 2);
- break;
- case '/':
- smart_str_appendl(buf, "\/" , 2);
- break;
-
- case 'b':
- smart_str_appendl(buf, "\b", 2);
- break;
-
- case 'f':
- smart_str_appendl(buf, "\f", 2);
- break;
-
- case 'n':
- smart_str_appendl(buf, "\n", 2);
- break;
-
- case 'r':
- smart_str_appendl(buf, "\r", 2);
- break;
-
- case 't':
- smart_str_appendl(buf, "\t", 2);
- break;
-
- case '<':
- if (options & PHP_JSON_HEX_TAG) {
- smart_str_appendl(buf, "\u003C", 6) ;
- } else {
- smart_str_appendc(buf, '<');
- }
- break;
-
- case '>':
- if (options & PHP_JSON_HEX_TAG) {
- smart_str_appendl(buf, "\u003E", 6);
- } else {
- smart_str_appendc(buf, '>');
- }
- break;
-
- case '&':
- if (options & PHP_JSON_HEX_AMP) {
- smart_str_appendl(buf, "\u0026", 6);
- } else {
- smart_str_appendc(buf, '&');
- }
- break;
-
- case ''':
- if (options & PHP_JSON_HEX_APOS) {
- smart_str_appendl(buf, "\u0027", 6);
- } else {
- smart_str_appendc(buf, ''');
- }
- break;
-
- default: //Up to here, the value will be appended to buf without special characters
- if (us >= ' ' && (us & 127) == us) {
- smart_str_appendc(buf, (unsigned char) us);
- }else {
- smart_str_appendl(buf, "\u", 2);
- us = REVERSE16(us);
-
- smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
- us >>= 4;
- smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
- us >>= 4;
- smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
- us >>= 4;
- smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]);
- }
- break;
- }
- }
- smart_str_appendc(buf, '"'); //结束 双引号。
- efree(utf16);
- }
复制代码Let’s take a look at arrays and objects. It’s also very simple.
- static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) /* {{{ */
- {
- int i, r;
- HashTable *myht;
-
- if (Z_TYPE_PP(val) == IS_ARRAY) {
- myht = HASH_OF(*val);
- r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val TSRMLS_CC);
- } else {
- myht = Z_OBJPROP_PP(val);
- r = PHP_JSON_OUTPUT_OBJECT;
- }
-
- if (myht && myht->nApplyCount > 1) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
- smart_str_appendl(buf, "null", 4);
- return;
- }
- //开始标签
- if (r == PHP_JSON_OUTPUT_ARRAY) {
- smart_str_appendc(buf, '[');
- } else {
- smart_str_appendc(buf, '{');
- }
-
- i = myht ? zend_hash_num_elements(myht) : 0;
-
- if (i > 0)
- {
- char *key;
- zval **data;
- ulong index;
- uint key_len;
- HashPosition pos;
- HashTable *tmp_ht;
- int need_comma = 0;
-
- zend_hash_internal_pointer_reset_ex(myht, &pos);
- //便利哈希表
- for (;; zend_hash_move_forward_ex(myht, &pos)) {
- i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
- if (i == HASH_KEY_NON_EXISTANT)
- break;
-
- if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS) {
- tmp_ht = HASH_OF(*data);
- if (tmp_ht) {
- tmp_ht->nApplyCount++;
- }
-
- if (r == PHP_JSON_OUTPUT_ARRAY) {
- if (need_comma) {
- smart_str_appendc(buf, ',');
- } else {
- need_comma = 1;
- }
- //将值append到 buf中
- php_json_encode(buf, *data, options TSRMLS_CC);
- } else if (r == PHP_JSON_OUTPUT_OBJECT) {
- if (i == HASH_KEY_IS_STRING) {
- if (key[0] == '
|