JSONLite 簡化版的 JSON
介紹
JSONLite 是 JSON 的簡化版。減少字符輸出的同時,仍保持數據有效性。
建議PHP版本 >= 5.2.0 。
特性
-
Js 兼容模式,兼容Js語法。取消了不必要的雙引號。
-
Strict 強類型模式,提供強類型輸出與解析,可用于與強類型語言通訊。
-
如 1.0 序列化和解序列后的類型均為 double,不會轉換為 int 1。
-
Min 最小化模式,最小化輸出數據,可用于日志打印。
-
較為精確的錯誤位置和信息提示。
-
解析時更為顯性的暴漏格式錯誤
示例代碼:
require_once 'jsonlite.php'; $value = array( 'code' => '123', 'msg' => 'true str', 'null' => null, 'new' => '', 'double' => 1.0, ); // 序列化 // Js兼容模式 $encoder = new JSONLiteEncoder($value); echo $encoder->encode(), PHP_EOL; // {code:"123",msg:"true str","null":null,"new":"",double:1} // Strict強類型模式 $encoder = new JSONLiteEncoder($value, JSONLiteEncoder::TYPE_STRICT); echo $encoder->encode(), PHP_EOL; // {code:"123",msg:true str,"null":null,new:,double:1.0} // Min最小化模式 $encoder = new JSONLiteEncoder($value, JSONLiteEncoder::TYPE_MIN); echo $encoder->encode(), PHP_EOL; // {code:123,msg:true str,"null":null,new:,double:1} // 解序列 $jsonlite = '{code:123,msg:true str,"null":null,new:,double:1}'; $encoder = new JSONLiteDecoder($jsonlite); var_export($encoder->decode()); /** * array ( * 'code' => 123, * 'msg' => 'true str', * 'null' => NULL, * 'new' => '', * 'double' => 1, * ) */
體積對比
根據測試數據計算,實際情況請另行估算。
模式 | JSON | JSONLite | 變化量 | 變化率 |
array_js | 92 | 92 | 0 | 0.00% |
array_strict | 92 | 74 | -18 | 19.57% |
array_min | 92 | 70 | -22 | 23.91% |
map_js | 111 | 97 | -14 | 12.61% |
map_strict | 111 | 83 | -28 | 25.23% |
map_min | 111 | 81 | -30 | 27.03% |
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!