This repository was archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
LUtilsJson reference
Lexize edited this page Sep 8, 2022
·
1 revision
LUtils submodule for work with JSON strings. Can be accessed with lutils.json.
toJson(LuaValue value): String
Converts provided value into JSON string.
Also there is some rule's that relates to JSON objects and JSON arrays:
If you'll provide table, which keys is integers only, and more than 0, it will be converted into JSON array.
Otherwise your table will be converted into JSON object, with all your keys converted to strings.
fromJson(String json): LuaValue
Converts provided string into lua value.
How to use:
local json = lutils.json;
local testTable = {};
testTable["foo"] = "bar";
testTable["testArray"] = {};
testTable["testArray"][1] = "foo";
testTable["testArray"][2] = "bar";
testTable["wrongArray"] = {};
testTable["wrongArray"]["test"] = "foo";
testTable["wrongArray"][2] = "bar";
testTable["wrongArray"][1.5] = "test";
local testString = json:toJson(testTable);
print(testString);
-- Output:
-- {
-- "wrongArray": {
-- "2.0": "bar",
-- "test": "foo",
-- "1.5": "test"
-- },
-- "testArray": [
-- "foo",
-- "bar"
-- ],
-- "foo": "bar"
-- }
local outTestTable = json:fromJson(testString);
printTable(outTestTable, 2);
-- Output:
-- table: {
-- ["wrongArray"] = table: {
-- ["test"] = "foo"
-- ["2.0"] = "bar"
-- ["1.5"] = "test"
-- }
-- ["testArray"] = table: {
-- [1] = "foo"
-- [2] = "bar"
-- }
-- ["foo"] = "bar"
-- }