json

class json

Native JSON module that can act as a drop in replacement for dkjson

null: lightuserdata

value used to indicate a table value should be encoded as a null json value.

array_mt: table

metatable used to indicate that a table should be encoded as a json array rather than a json object.

static encode(val, ...)

Encode a table as a JSON string.

Notes on compatibility with dkjson.encode (object [, state]):

  • The optional state table available in dkjson is not available in the native json module.

  • Empty tables are encoded as a json object by default for the native json module, but as an array by default for dkjson.

  • The native json module uses the array_mt set as a metatable to force encoding as a json array, while dkjson uses another mechanism.

  • However, like dkjson, if #tbl is greater than 0, the table will be encoded as a json array.

Parameters
  • val (table) – the table to encode as a string

  • vararg (any) – for API compatibility w/ dkjson, currently unused by native json module

Returns

the serialized JSON string

Return type

str

static decode(str, ...)

Decode a JSON string into a Lua table.

Errors if the json string is invalid json (note that dkjson returns error messages rather than erroring in Lua). Null json values are decoded as nil by default, but can be decoded as json.null lightuserdata if specified in the nullval argument.

Notes on compatability with dkjson.decode (string [, position [, null [, objectmeta [, arraymeta]]]]):

  • position argument is not supported, the entire string will always be decoded

  • null argument only supports the native json.null lightuserdata value; anything else will be treated as if it were default

  • no metatables are added to tables decoded from json arrays/objects

Parameters
  • str (str) – the JSON string to deserialize

  • vararg (any) – for API compatibility w/ dkjson, nullval argument can only be json.null for native json, default is nil

Returns

the Lua table representation of the JSON string

Return type

table

class JsonModule

The json module is what is required into a driver. It loads a native json module when running on target, or dkjson, a pure lua implementation, when the native json impl is not available (i.e running driver unit tests). dkjson can be used on target by requiring it instead of this module, but the native json module has much better performance than dkjson in terms of memory usage. The api’s are mostly interchangable, however, not all options in dkjson are available in the native json module. See https://github.com/LuaDist/dkjson for dkjson documentation, and the Edge Driver reference for the native json documentation.

configure(loader_args)

Load the JSON Lua module based on the given loader args

Parameters

loader_args (LoaderArgs) – configuration paramters for the Lua module