Home  Contents

table.fromJsonSchema

JSON Core4 Lua Commands

SYNOPSIS

schema = table.fromJsonSchema(text)

DESCRIPTION

This is a companion function to table.toJson().

Decodes a JSON formatted schema description into a lua table. The resulting table can be stored and used with table.toJson() to generate JSON data.

The decoder works almost identical to table.fromJson(). The only difference is when a JSON object with the name properties is encountered. The decoder will decode this into a Lua array instead of a name=value table.

RETURN VALUE

On success, returns a lua table with the decoded schema.

On failure, returns two values: nil and the index into the string where the first error was encountered.

EXAMPLES

>  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  >  > 
jsonschema = [[{ "$schema": "http://json-schema.org/schema#", "name": "Product", "type": "object", "properties": { "id": { "type": "number", }, "name": { "type": "string", }, "price": { "type": "number", }, "tags": { "type": "array", "items": { "type": "string" } }, "stock": { "type": "object", "properties": { "warehouse": { "type": "number" }, "retail": { "type": "number" } } } } }]] print(table.tostring(table.fromJsonSchema(jsonschema)))
{ name = "Product", type = "object", properties = { { name = "id", type = "number" }, { name = "name", type = "string" }, { name = "price", type = "number" }, { name = "tags", type = "array", items = { type = "string" } }, { name = "stock", type = "object", properties = { { name = "warehouse", type = "number" }, { name = "retail", type = "number" } } } }, ["$schema"] = "http://json-schema.org/schema#" }
Output has been reformatted for clarity.

SEE ALSO