Skip to content

wax json

Thadeu de Paula edited this page Feb 6, 2023 · 3 revisions

wax.json

Create and parse JSON file to/from Lua tables.

  local json = require 'wax.json'
wax.json.encode
  • wax.json.encode( t: {} ) : string

Convert the table t into a JSON string.

wax.json.decode
  • wax.json.decode( jsonstr: string) : table

Convert the jsonstr string into a Lua table.

  --| Every non array or object is converted to respective Lua
  --| counterpart:
  assert(json.decode[["hi"]] == "hi")
  assert(json.decode[[null]] == json.null)
  assert(json.decode[[10.9]] == 10.9)
  assert(json.decode[[109]]  == 109)
  assert(json.decode[[true]] == true)
  assert(json.decode[[false]] == false)

  local object = json.decode([[{
  	"str":"A string", "num":10.667, "int":70999,
  	"boot": true, "boof": false, "nul":null,
  	"arr":["a", "b"], "obj":{"k":"v"}
  }]])
  assert(object.str   == 'A string')
  assert(object.num   == 10.667)
  assert(object.int   == 70999)
  assert(object.boot  == true)
  assert(object.boof  == false)
  assert(object.nul   == json.null)
  assert(#object.arr  == 2)
  assert(object.obj.k == "v")
Clone this wiki locally