Decode JSON to Lua table.
value = rapidjson.decode(jsonstring)
value = rapidjson.decode(lightuserdata, length)
jsonstring
A JSON value string to be decoded.
lightuserdata
The C buffer start address.
length
The C buffer length.
Return table if JSON is an object or array.
Return true
, false
, number and rapidjson.null
respectively if JSON is a simple value.
Return nil plus an error message as a second result when passed string is not a valid JSON.
- When passed value is not (convertible to) string.
Encode Lua table to JSON string.
supports the following types:
- boolean
- rapidjson.null
- number
- string
- table
The JSON object keys are sorted by the this function.
string = rapidjson.encode(value [, option])
value:
When passed a table:
- it is encoded as JSON array if:
- meta field
__jsontype
set toarray
. - table contains length > 0.
- meta field
- otherwise the table is encoded as JSON object and non string keys and its values are ignored.
When passed with true
, false
, number and rapidjson.null
, simply encode as simple JSON value.
option:
A optional table contains follow field:
pretty
boolean: Settrue
to make output string to be pretty formated. Default is false.sort_keys
boolean: Settrue
to make JSON object keys be sorted. Default isfalse
.empty_table_as_array
boolean: Settrue
to make empty table encode as JSON array. Default isfalse
.
Return stringified JSON on success. Return nil on failure, plus an error message as a second result.
- When option passed a value other than table.
local rapidjson = require('rapidjson')
rapidjson.encode({}) -- '{}'
rapidjson.encode(rapidjson.object()) --> '{}'
rapidjson.encode(rapidjson.array()) --> '[]'
rapidjson.encode(setmetatable({}, {__jsontype='object'})) --> '{}'
rapidjson.encode(setmetatable({}, {__jsontype='array'})) --> '[]'
rapidjson.encode(true) --> 'true'
rapidjson.encode(rapidjson.null) --> 'null'
rapidjson.encode(123) --> '123.0' or '123' in Lua 5.3.
rapidjson.encode({true, false}) --> '[true, false]'
rapidjson.encode({a=true, b=false}) --> '{"a":true,"b":false]'
Load JSON file into Lua table.
value = rapidjson.load(filename)
filename
JSON file to be loaded.
Return table if file contains an object or array.
Return true
, false
, number and rapidjson.null
respectively if file contains a simple value.
Return nil plus an error message as a second result when passed file is not valid JSON file.
- When passed filename is not (convertible to) string.
Dump Lua value to JSON file.
success, err = rapidjson.dump(value, filename [, option])
value
Same as in rapidjson.encode()
.
filename
The file path string where to save encode json string.
option:
Same as in options in rapidjson.encode()
.
bool: success
Return true on success.
Return false plus an error message as a second result when:
- Value can't be encoded.
filename
can't be opened for write.
- When passed filename is not (convertible to) string.
- When passed option is not table, nil or none.
local rapidjson = require('rapidjson')
rapidjson.dump({rapidjson.null}, 'test.json')
rapidjson.dump({rapidjson.null}, 'test-pretty.json', {pretty=true})
The placeholder for null values in rapidjson.
eg.
local rapidjson = require('rapidjson')
rapidjson.decode('[null]') --> {rapidjson.null}
rapidjson.encode({rapidjson.null}) --> '[null]'
Create a new empty table that have metatable field __jsontype
set as 'object'
so that the encode
and dump
function will encode it as JSON object.
When passed an valid table:
- Passed table do not have metatable, just set above metatable for the table.
- Passed table already have metatable, set the metatable field
__jsontype
to 'object'.
obj = rapidjson.object([t])
t
Optional table to be set the metatable with meta field __jsontype
set as 'object'
.
Origin passed in table when passed with a table. Or new created table.
Same as rapidjson.array() except the metatable field __jsontype
is set as 'array'
. And the encode
and dump
function will encode it as JSON array.
Creates a rapidjson Document object. Optionally create from a Lua table or string of JSON document.
doc = rapidjson.Document([t|s])
t
Optional table to be create a rapidjson Document from.
s
Optional a string contains a JSON document, then when document created the string is parsed into the document.
Parse JSON document contained in string s.
local ok, message = document:parse(s)
s
A string contains a JSON document.
Returns true
on success. Otherwise false
and an additional error message is returned.
local rapidjson = require('rapidjson')
local doc = rapidjson.Document()
local ok, message = doc:parse('{"a":["appke", "air"]}')
if not ok then
print(message)
end
Get document member by JSON Pointer.
local value = document:get(pointer[, default])
pointer
A string contains JSON pointer.
default
The default value to return if the document does not contains value specified by the pointer.
It document have elements specified by pointer, the element value is returned as a Lua value.
Otherwise, default
value is returned; if default
is not specified, nil
is returned.
Set document member by JSON Pointer with specified value.
document:set(pointer, value)
pointer
A string contains JSON pointer.
value
The value to set as new value for document element specified by pointer.
local doc = rapidjson.Document()
doc:set('/a', {'apple', 'air'})
Creates a SchemaDocument from Document or a Lua table or a string contains a JSON schema.
local sd = rapidjson.SchemaDocument(doc|t|s)
doc
The the JSON schema stored in rapidjson.Document object.
t
The Lua table representation of a JSON schema.
s
The string contains a JSON schema.
Returns the new SchemaDocument object.
local d = rapidjson.Document('{"type": ["string", "number"]}')
local sd = rapidjson.SchemaDocument(d)
local sd = rapidjson.SchemaDocument({type= {"string", "number"}})
local sd = rapidjson.SchemaDocument('{"type": ["string", "number"]}')
Creates a SchemaValidator from a SchemaDocument.
local validator = apidjson.SchemaValidator(sd)
sd
The SchemaDocument to create the validator. SchemaDocument can be shared by schema validators.
Returns the new created validator object.
local sd = rapidjson.SchemaDocument('{"type": ["string", "number"]}')
local validator = rapidjson.SchemaValidator(sd)
local d = rapidjson.Document('.....')
local ok, message = validator:validate(d)
Validates a JSON document.
local ok, message = validator:validate(d)
d
The document to be validated against the schema stored inside the validator.
Returns true
if the document is valid. Otherwise returns false
and a extra error message.
A string that is "rapidjson"
.
The current loaded rapidjson version. "scm"
when not build with luarocks.