Skip to content

kill8g/lua-tablelib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

lua-tablelib

tablelib是一个使用C++实现的动态库,提供给lua使用。

tablelib is a dynamic library implemented in C++ for lua.

Installation

将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.

Usage

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",},}

Notes

  • tostring函数可以接收任意参数。如果参数不是一个table,它会返回参数的字符串表示。
  • The tostring function can accept any argument. If the argument is not a table, 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, then tostring 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 a table to a json format string. To solve the problem of sparse numeric key in table, it will convert all numeric key (including floating point numbers) to string, and add _int_keys_ field. It is an array that records which numeric key 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 a json format string to a table. If the _int_keys_ field exists, it will iterate through _int_keys_ and convert the corresponding key to number.
  • 如果你的table中存在相同名字的number keystring key, 那么tojson转换后的结果将是错误的, 例如 : {[1] = "number", ["1"] = "string"}, 转为json后 : {"1":"string","1":"number","_int_keys_":["1"]}
  • If your table contains number key and string key with the same name, then the result of tojson conversion will be wrong, for example: {1 = "number", ["1"] = "string"}, converted to json: {"1":"string","1":"number","int_keys":["1"]}.

About

😼 tablelib is a dynamic library implemented in C++ for lua.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published