LUA 3rd party libraries

LUA 3rd party libraries

JSON related

Whenever something is related to JSON, the library json4lua provided by Egor-Skriptunoff has turned out to be helpful. It converts JSON data to a LUA object and vice versa. It can be simply used within a workspace by adding one file to the ScriptFiles folder and by adding one require statement:

  • Add to the ScriptFiles folder.

  • Insert the statement json = require('json') e.g. into the file nxaDefinitions.lua.

  • Use the functions json.encode() or json.decode().

The library works with LUA 5.1 or 5.4. Example code to study (can be tried in Execute Script dialog in Core Studio):

encodedJsonString = '{ \ a:5, \ b:"asdf", \ c:null, \ "d d":123456789000.234567890123456789, \ anObject:{f:"inner value", g:{}, h:"alt notation"}, \ anArray:[111, 2222, null, {x:99}, [101,202,303]] \ }' data = json.decode(encodedJsonString) nxa.LogInfo(data["doesNotExist"]) -- the key does not exist in json string nxa.LogInfo(data["doesNotExist"] == nil) -- is true nxa.LogInfo(data["doesNotExist"] == json.null) -- if false nxa.LogInfo("") nxa.LogInfo(data["a"]) nxa.LogInfo(data["b"]) nxa.LogInfo(data["c"] == nil) -- the key exists, but avoid this to check if the value is null nxa.LogInfo(data["c"] == json.null) -- use this instead if you need to compare null values nxa.LogInfo(data["d d"]) nxa.LogInfo("") nxa.LogInfo(data["anObject"]["f"]) nxa.LogInfo(data["anObject"]["f"] == json.empty) -- if false nxa.LogInfo(data["anObject"]["g"] == json.empty) -- if true nxa.LogInfo("") nxa.LogInfo(data["anArray"][1]) nxa.LogInfo(data["anArray"][2]) nxa.LogInfo(data["anArray"][3] == json.null) nxa.LogInfo(data["anArray"][4]['x']) nxa.LogInfo(data["anArray"][5][2]) nxa.LogInfo("") nxa.LogInfo(data.anObject.h) -- alternative notation nxa.LogInfo("") nxa.LogInfo(json.encode(data)) nxa.LogInfo("") nxa.LogInfo(json.encode({x = {json.null, {y = "inner deep value"}, json.null}})) -- use json.null if a json value must be 'null'. 'nil' will not work!