- 编码为 array 还是 object
编码为 array 还是 object
首先大家请看这段源码:
-- http://www.kyne.com.au/~mark/software/lua-cjson.php-- version: 2.1 devellocal json = require("cjson")ngx.say("value --> ", json.encode({dogs={}}))
输出结果
value —> {“dogs”:{}}
注意看下 encode 后 key 的值类型,”{}” 代表 key 的值是个 object,”[]” 则代表 key 的值是个数组。对于强类型语言(C/C++, Java 等),这时候就有点不爽。因为类型不是他期望的要做容错。对于 Lua 本身,是把数组和字典融合到一起了,所以他是无法区分空数组和空字典的。
参考 openresty/lua-cjson 中额外贴出测试案例,我们就很容易找到思路了。
-- 内容节选lua-cjson-2.1.0.2/tests/agentzh.t=== TEST 1: empty tables as objects--- lualocal cjson = require "cjson"print(cjson.encode({}))print(cjson.encode({dogs = {}}))--- out{}{"dogs":{}}=== TEST 2: empty tables as arrays--- lualocal cjson = require "cjson"cjson.encode_empty_table_as_object(false)print(cjson.encode({}))print(cjson.encode({dogs = {}}))--- out[]{"dogs":[]}
综合本章节提到的各种问题,我们可以封装一个 json_encode 的示例函数:
local json = require("cjson")--稀疏数组会被处理成objectjson.encode_sparse_array(true)local function _json_encode(data)return json.encode(data)endfunction json_encode( data, empty_table_as_object )--Lua的数据类型里面,array和dict是同一个东西。对应到json encode的时候,就会有不同的判断--cjson对于空的table,就会被处理为object,也就是{}--处理方法:对于cjson,使用encode_empty_table_as_object这个方法。json.encode_empty_table_as_object(empty_table_as_object or false) -- 空的table默认为arraylocal ok, json_value = pcall(_json_encode, data)if not ok thenreturn nilendreturn json_valueend
另一种思路是,使用 setmetatable(data, json.empty_array_mt),来标记特定的 table,让 cjson 在编码这个空 table 时把它处理成 array:
local data = {}setmetatable(data, json.empty_array_mt)ngx.say("empty array: ", json.encode(data)) -- empty array: []
