tablelib是一个使用C++实现的动态库,提供给lua使用。
tablelib is a dynamic library implemented in C++ for lua.
将tablelib.dll或tablelib.so文件放到你的lua项目的目录下,或者添加到你的环境变量中。
Put the tablelib.dll or tablelib.so file in your lua project directory, or add it to your environment variables.
local tablelib = require "tablelib"
-- tostring
local s = tablelib.tostring({value = 1})
print(s)
-- {value = 1}
-- tojson
local tb = {["value"]=1,["array"]={[1]=1,[2]=2,},["hash"]={[100]="string",},}
local json = tablelib.tojson(tb)
print(json)
-- {"array":{"1":1,"2":2,"_int_keys_":["1","2"]},"value":1,"hash":{"100":"string","_int_keys_":["100"]}}
-- loadjson
local tb = tablelib.loadjson(json)
print(tablelib.tostring(tb))
-- {["value"]=1,["array"]={[1]=1,[2]=2,},["hash"]={[100]="string",},}
tostring
函数可以接收任意参数。如果参数不是一个table
,它会返回参数的字符串表示。- The
tostring
function can accept any argument. If the argument is not atable
, it will return the string representation of the argument. tostring
函数可以安全地处理table中的循环引用。它会标记已访问过的元素,避免无限递归。- The
tostring
function can handle circular references in tables safely. It will mark the visited elements and avoid infinite recursion. - 如果你在编译库时添加了
-D_OPEN_HEX_
选项,它会把数值类型转为16进制字符串,性能优于10进制字符串。 - If you compile the library with the
-D_OPEN_HEX_
option, it will convert numeric values to hexadecimal strings, which have better performance than decimal strings. - 如果你的
table
有__pairs
元方法,那么tostring
tojson
可能无法按照你的预期工作,它不会调用__pairs
元方法来遍历table。 - If your table has a
__pairs
metamethod, thentostring
tojson
may not work as you expect. It will not call the__pairs
metamethod to iterate over the table. tojson
函数会将一个table
转为json
格式的字符串, 为了解决table
数值key
过于稀疏的问题, 会将所有的数值key
(包括浮点数)转为string
, 并添加_int_keys_
字段, 它是一个数组, 会记录有哪些数值key
被转换, 例如 :{["value"]=1,["array"]={[1]=1,[2]=2,},["hash"]={[100]=999,},}
将会被转为:{"array":{"1":1,"2":2,"_int_keys_":["1","2"]},"value":1,"hash":{"100":999,"_int_keys_":["100"]}}
- The
tojson
function will convert atable
to ajson
format string. To solve the problem of sparse numeric key intable
, it will convert all numerickey
(including floating point numbers) tostring
, and add_int_keys_
field. It is an array that records which numerickey
are converted1. For example :{["value"]=1,["array"]={[1]=1,[2]=2,},["hash"]={[100]=999,},}
converted to :{"array":{"1":1,"2":2,"_int_keys_":["1","2"]},"value":1,"hash":{"100":999,"_int_keys_":["100"]}}
loadjson
函数会将json
格式的字符串转为table
, 如果存在_int_keys_
字段, 会遍历_int_keys_
, 将对应的key
转为number
- The
loadjson
function will convert ajson
format string to atable
. If the_int_keys_
field exists, it will iterate through_int_keys_
and convert the correspondingkey
tonumber
. - 如果你的
table
中存在相同名字的number key
和string key
, 那么tojson
转换后的结果将是错误的, 例如 :{[1] = "number", ["1"] = "string"}
, 转为json
后 :{"1":"string","1":"number","_int_keys_":["1"]}
- If your
table
containsnumber key
andstring key
with the same name, then the result oftojson
conversion will be wrong, for example:{1 = "number", ["1"] = "string"}
, converted to json:{"1":"string","1":"number","int_keys":["1"]}
.