json的優點就不說了,

有個習慣,我在輸出json的時候,喜歡用 sprintf 拼成json格式,

前兩天被朋友說不標準,必須要用json_encode生成的才是標準的json格式,我當然很鬱悶啦,

用了這麼多年了,剛知道 這樣做不標準,既然說我不標準,那上面才是標準的json格式?

  1. {a : `abc`
  2. {`a` : `abc`
  3. {a : "abc"
  4. {"a" : "abc"

那都知道,只有第四種才是標準的json格式。

我這麼做

  1. $ret_json=`{"%s":"%s"}`
  2. echo sprintf($ret_json,"a","abc"); 

必然也符合標準。

既然如此,那我就要刨根問底,json_encode生成的json格式究竟有什麼不同?

上程式碼

  1. static PHP_FUNCTION(json_encode) 
  2.         zval *parameter; 
  3.         smart_str buf = {0}; 
  4.         long options = 0; 
  5.   
  6.         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &parameter, &options) == FAILURE) { 
  7.                 return
  8.         }    
  9.   
  10.         JSON_G(error_code) = PHP_JSON_ERROR_NONE; 
  11.   
  12.         php_json_encode(&buf, parameter, options TSRMLS_CC); 
  13.   
  14.         ZVAL_STRINGL(return_value, buf.c, buf.len, 1);  
  15.   
  16.         smart_str_free(&buf); 



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.         switch (Z_TYPE_P(val)) 
  3.         { 
  4.                 case IS_NULL: 
  5.                         smart_str_appendl(buf, "null", 4); //輸出NULL 
  6.                         break
  7.   
  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.   
  16.                 case IS_LONG: 
  17.                         smart_str_append_long(buf, Z_LVAL_P(val));//輸出長整形的值 
  18.                         break
  19.   
  20.                 case IS_DOUBLE: 
  21.                         { 
  22.                                 char *d = NULL; 
  23.                                 int len; 
  24.                                 double dbl = Z_DVAL_P(val); 
  25.   
  26.                                 if (!zend_isinf(dbl) && !zend_isnan(dbl)) {//非無窮盡 
  27.                                         len = spprintf(&d, 0, "%.*k", (int) EG(precision), dbl); 
  28.                                         smart_str_appendl(buf, d, len); 
  29.                                         efree(d); 
  30.                                 } else { 
  31.                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", dbl); 
  32.                                         smart_str_appendc(buf, `0`); 
  33.                                 } 
  34.                        } 
  35.                         break
  36.   
  37.                 case IS_STRING://字串 
  38.                         json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC); 
  39.                         break
  40.   
  41.                 case IS_ARRAY://陣列和物件 
  42.                 case IS_OBJECT: 
  43.                         json_encode_array(buf, &val, options TSRMLS_CC); 
  44.                         break
  45.   
  46.                 default
  47.                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported, encoded as null"); 
  48.                         smart_str_appendl(buf, "null", 4); 
  49.                         break
  50.         } 
  51.   
  52.         return

很明顯,根據不同的型別,會有相應的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.         int pos = 0; 
  4.         unsigned short us; 
  5.         unsigned short *utf16; 
  6.   
  7.         if (len == 0) {//如果長度為0,則直接返回 雙引號 "" 
  8.                 smart_str_appendl(buf, """", 2); 
  9.                 return
  10.         } 
  11.   
  12.         if (options & PHP_JSON_NUMERIC_CHECK) {//檢測是否為0-9的數字,如果是數字,那麼就會直接把資料作為long或double型別返回。 
  13.                 double d; 
  14.                 int type; 
  15.                 long p; 
  16.   
  17.                 if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) { 
  18.                         if (type == IS_LONG) { 
  19.                                 smart_str_append_long(buf, p); 
  20.                         } else if (type == IS_DOUBLE) { 
  21.                                 if (!zend_isinf(d) && !zend_isnan(d)) { 
  22.                                         char *tmp; 
  23.                                         int l = spprintf(&tmp, 0, "%.*k", (int) EG(precision), d); 
  24.                                         smart_str_appendl(buf, tmp, l); 
  25.                                         efree(tmp); 
  26.                                 } else { 
  27.                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", d); 
  28.                                         smart_str_appendc(buf, `0`); 
  29.                                 } 
  30.                         } 
  31.                         return
  32.                 } 
  33.   
  34.         } 
  35.   
  36.         utf16 = (unsigned short *) safe_emalloc(len, sizeof(unsigned short), 0); 
  37.         len = utf8_to_utf16(utf16, s, len); //這裡會對你輸入的值一次處理轉成對應的Dec碼,比如1是49,a是97這樣的,儲存到utf16中。 
  38.         if (len <= 0) {//如果len小於0 說明出錯。如果用json_encode處理GBK的編碼,就會在這裡掛掉。 
  39.                 if (utf16) { 
  40.                         efree(utf16); 
  41.                 } 
  42.                 if (len < 0) { 
  43.                         JSON_G(error_code) = PHP_JSON_ERROR_UTF8; 
  44.                         if (!PG(display_errors)) { 
  45.                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument"); 
  46.                         } 
  47.                         smart_str_appendl(buf, "null", 4); 
  48.                 } else { 
  49.                         smart_str_appendl(buf, """", 2); 
  50.                 } 
  51.                 return
  52.         } 
  53.   
  54.         smart_str_appendc(buf, `"`); //輸入 " 
  55.   
  56. //下面這一段程式碼就是將一些特殊字元轉義如 雙引號,反斜線等等 
  57.         while (pos < len) 
  58.         { 
  59.                 us = utf16[pos++]; 
  60.   
  61.                 switch (us) 
  62.                 { 
  63.                         case `"`
  64.                                 if (options & PHP_JSON_HEX_QUOT) { 
  65.                                         smart_str_appendl(buf, "\u0022", 6); 
  66.                                 } else { 
  67.                                         smart_str_appendl(buf, "\"", 2); 
  68.                                 } 
  69.                                 break
  70.   
  71.                         case `\`
  72.                                 smart_str_appendl(buf, "\\", 2); 
  73.                                 break
  74. case `/`
  75.                                 smart_str_appendl(buf, "\/", 2); 
  76.                                 break
  77.   
  78.                         case ``
  79.                                 smart_str_appendl(buf, "\b", 2); 
  80.                                 break
  81.   
  82.                         case `f`
  83.                                 smart_str_appendl(buf, "\f", 2); 
  84.                                 break
  85.   
  86.                         case ` `
  87.                                 smart_str_appendl(buf, "\n", 2); 
  88.                                 break
  89.   
  90.                         case ` `
  91.                                 smart_str_appendl(buf, "\r", 2); 
  92.                                 break
  93.   
  94.                         case ` `
  95.                                 smart_str_appendl(buf, "\t", 2); 
  96.                                 break
  97.   
  98.                         case `<`
  99.                                 if (options & PHP_JSON_HEX_TAG) { 
  100.                                         smart_str_appendl(buf, "\u003C", 6); 
  101.                                 } else { 
  102.                                         smart_str_appendc(buf, `<`); 
  103.                                 } 
  104.                                 break
  105.   
  106.                         case `>`
  107.                                 if (options & PHP_JSON_HEX_TAG) { 
  108.                                         smart_str_appendl(buf, "\u003E", 6); 
  109.                                 } else { 
  110.                                         smart_str_appendc(buf, `>`); 
  111.                                 break
  112.   
  113.                         case `&`
  114.                                 if (options & PHP_JSON_HEX_AMP) { 
  115.                                         smart_str_appendl(buf, "\u0026", 6); 
  116.                                 } else { 
  117.                                         smart_str_appendc(buf, `&`); 
  118.                                 } 
  119.                                 break
  120.   
  121.                         case ```
  122.                                 if (options & PHP_JSON_HEX_APOS) { 
  123.                                         smart_str_appendl(buf, "\u0027", 6); 
  124.                                 } else { 
  125.                                         smart_str_appendc(buf, ```); 
  126.                                 } 
  127.                                 break
  128.   
  129.                         default//一直到這裡,沒有特殊字元就會把值append到buf中 
  130.                                 if (us >= ` ` && (us & 127) == us) { 
  131.                                         smart_str_appendc(buf, (unsigned char) us); 
  132.                                 } else { 
  133.                                         smart_str_appendl(buf, "\u", 2); 
  134.                                         us = REVERSE16(us); 
  135.   
  136.                                         smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]); 
  137.                                         us >>= 4; 
  138.                                         smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]); 
  139.                                         us >>= 4; 
  140.                                         smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]); 
  141.                                         us >>= 4; 
  142.                                         smart_str_appendc(buf, digits[us & ((1 << 4) - 1)]); 
  143.                                 } 
  144.                                 break
  145.                 } 
  146.         } 
  147.         smart_str_appendc(buf, `"`); //結束 雙引號。 
  148.         efree(utf16); 

再來看看陣列和物件,也很簡單,

  1. static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) /* {{{ */ 
  2.         int i, r; 
  3.         HashTable *myht; 
  4.   
  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.   
  13.         if (myht && myht->nApplyCount > 1) { 
  14.                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected"); 
  15.                 smart_str_appendl(buf, "null", 4); 
  16.                 return
  17.         } 
  18. //開始標籤 
  19.         if (r == PHP_JSON_OUTPUT_ARRAY) { 
  20.                 smart_str_appendc(buf, `[`); 
  21.         } else { 
  22.                 smart_str_appendc(buf, `{`); 
  23.         }    
  24.   
  25.         i = myht ? zend_hash_num_elements(myht) : 0; 
  26.   
  27.         if (i > 0) 
  28.         { 
  29.                 char *key; 
  30.                 zval **data; 
  31.                 ulong index; 
  32.                 uint key_len; 
  33.                 HashPosition pos; 
  34.                 HashTable *tmp_ht; 
  35.                 int need_comma = 0; 
  36.   
  37.                 zend_hash_internal_pointer_reset_ex(myht, &pos); 
  38. //便利雜湊表 
  39.                 for (;; zend_hash_move_forward_ex(myht, &pos)) { 
  40.                         i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos); 
  41.                         if (i == HASH_KEY_NON_EXISTANT) 
  42.                                 break
  43.   
  44.                         if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS) { 
  45.                                 tmp_ht = HASH_OF(*data); 
  46.                                 if (tmp_ht) { 
  47.                                         tmp_ht->nApplyCount++; 
  48.                                 } 
  49.   
  50.                                 if (r == PHP_JSON_OUTPUT_ARRAY) { 
  51.                                         if (need_comma) { 
  52.                                                 smart_str_appendc(buf, `,`); 
  53.                                         } else { 
  54.                                                 need_comma = 1; 
  55.                                         } 
  56. //將值append到 buf中 
  57.                                         php_json_encode(buf, *data, options TSRMLS_CC); 
  58.                                 } else if (r == PHP_JSON_OUTPUT_OBJECT) { 
  59.                                         if (i == HASH_KEY_IS_STRING) { 
  60.                                                 if (key[0] == ` ` && Z_TYPE_PP(val) == IS_OBJECT) { 
  61.                                                         /* Skip protected and private members. */ 
  62.                                                         if (tmp_ht) { 
  63.                                                                 tmp_ht->nApplyCount--; 
  64.                                                         } 
  65.                                                         continue
  66.                                                 } 
  67.   
  68.                                                 if (need_comma) { 
  69.                                                         smart_str_appendc(buf, `,`); 
  70.                                                 } else { 
  71.                                                         need_comma = 1; 
  72.                                                 } 
  73.   
  74.                                                 json_escape_string(buf, key, key_len - 1, options & ~PHP_JSON_NUMERIC_CHECK TSRMLS_CC); 
  75.                                                 smart_str_appendc(buf, `:`); 
  76.   
  77.                                                 php_json_encode(buf, *data, options TSRMLS_CC); 
  78.                                         } else { 
  79.                                                 if (need_comma) { 
  80.                                                         smart_str_appendc(buf, `,`); 
  81.                                                 } else { 
  82.                                                         need_comma = 1; 
  83.                                                 } 
  84.   
  85.                                                 smart_str_appendc(buf, `"`); 
  86.                                                 smart_str_append_long(buf, (long) index); 
  87.                                                 smart_str_appendc(buf, `"`); 
  88.                                                 smart_str_appendc(buf, `:`); 
  89.   
  90.                                                 php_json_encode(buf, *data, options TSRMLS_CC); 
  91.                                         } 
  92.                                 } 
  93.   
  94.                                 if (tmp_ht) { 
  95.                                         tmp_ht->nApplyCount--; 
  96.                                 } 
  97.                         } 
  98.                 } 
  99.         } 
  100. //結束標籤 
  101.         if (r == PHP_JSON_OUTPUT_ARRAY) { 
  102.                 smart_str_appendc(buf, `]`); 
  103.         } else { 
  104.                 smart_str_appendc(buf, `}`); 
  105.         } 

 

通過簡單分析,證明了一個問題,跟我上面用sprintf的方法其實是一樣的,都是拼接字串,

而且 為了效能,更應該鼓勵用sprintf來拼接json格式,

因為 json_encode會進行很多 迴圈操作,而且所消耗的效能是線性的 O(n)。 ^.^