Skip to content

Latest commit

 

History

History
44 lines (32 loc) · 1.27 KB

luajson.adoc

File metadata and controls

44 lines (32 loc) · 1.27 KB

Decoding and Encoding JSON-formatted data

The json module is used to decode and encode data in JSON-format (Javascript Object Notation).

Functions

decode(data [, nullHandling])

Decode JSON-encoded data. The optional argument ’nullHandling’ specifies how JSON-null values are mapped to Lua values.

  • ’json-null’ (the default) maps to a Lua table with a special ’JSON-null’-Metatable that can be detected using the isnull() function described below.

  • ’empty-string’ maps JSON-null values to an empty string, which can be useful in web-based applications where e.g. PostgreSQL is used to generate JSONndata which is then handed to a browser over a WebSocket or similar mechanism.

  • ’nil’ maps JSON-null values to Lua nil.

decode() returns the decoded values as Lua values or nil if an error occurs. If an error occured, a second return value contains the error message.

encode(data)

Encode data in JSON-encoding.

JSON-null values

JSON has a special datatype to denote no value: the JSON-null value. To insert a JSON-null value, assign json.null. Use the following function to test for JSON-null values.

isnull(var)

Returns true if var is JSON-null.