JSON
JSON allows you to encode a Lua table into a JSON string and decode a JSON string or Data into a Lua table.
Functions
Decode takes a json string or Data instance as parameter and returns a Lua table.
It also returns an error (string) when the provided string can't be decoded.
Numbers are automatically converted to integers when they have no decimal part.
local jsonstring = "{\"body\":\"Test message\",\"status_code\":200}" local data = JSON:Decode(jsonstring) print(data.body, "- status:", data.status_code) -- prints "Test message - status: 200" -- error handling: local jsonstring = "{\"body\":\"Test message\",\"status_code\":200" -- missing '}' at the end local data, err = JSON:Decode(jsonstring) if err ~= nil then print("could not decode jsonstring, error:", err) end
Takes a Lua table as parameter and returns a json encoded string.
This function handles basic types (number, string, boolean, tables) and skips unsupported types.
For arrays (tables with sequential integer keys), all values must be of supported types.
For objects (tables with string keys), values of unsupported types are skipped.
Supported types are: number, string, boolean, and table.
local playerInfo = {} playerInfo.hp = 100 playerInfo.name = "Bob" local encoded = JSON:Encode(playerInfo) print(encoded) -- prints the string {"hp":100,"name":"Bob"}