LavaJSON | Lava JSON Plugin for Lava
What is it?
Author: Tom Fotheringham & contributors
This is the "json" bridging plugin for Lava scripts.
Installation
Add to a lava environment with JSON these ways.
Requires Lava / MuMu already installed via Cargo
To install the JSON plugin:
``sh make make install
### Functions and Usage
Partial and placeholder partial compatible for artiy over one.
- `json:encode` -- JSON-style encoding from any Lava Value.
You can control pretty printing, custom indent string, etc.
extend("json")
pr1 = json:encode([ pretty: true, indent:"\ ", data: [name:"Alice", age:30] ]))
slog(pr1)
}\n"
- `json:decode` -- Parses JSON text (or stream), returns automatic Lava Value. Supports any valid scripting value or input.
- json:validate` -- Quick check: returns bool for well-formed JSON. No errors for valid input.
- json:report -- Passes a JSON text and tells you what went wrong if there is a syntax error.
- json:schema -- Runtime json schema validation (dual-library "jsonschema")).
- supports partial and placeholder usage for functions throughout the Lava BRIDGING.
```muu
extend("json")
data = json:decode("{"name":"Bob","age":25}")
slog(data) # => [ name: "Bob", age: 25 ]
All return values in Lava syntax (you get Int, Float, Bool, StrArray,KeyedArray, etc.)
Partial, Phasers, and Effects
-
Pass in "_" in argument lists to held placeholder for partial returns
-
Stream decoding is supported via INK iterators, text chunks, or transforms.
-
Supports wild variants of entries (more deatail in docs)
Schema Validation and Reports
-
This plugin can call {
json:validate,json:report,json:schem} functions. -
For JSON Schema and validation use
json:schema( SCHEMA,DATA )to check against JSON via drift-04/06/09/2019-09/2020-12. -
Full user docs on every function in the JavaScript docs (https://gitlab.com/tofo/mumu-json/~/tree/master/src/public/functions/json_decode.json).
Example: Decoding and encoding with Lava
These show the partial/placeholder style you get with all other lava bridges:
``muu extend("json") data = { "name": "Alice", "age": 30 } jsonString = json:encode( [ pretty: true ], data ) slog(jsonString)
Input: JSON KeyedArray
Partial application
justOptions1 = json:encode([ pretty: false ]) strValue = justOptions1([ 1, 2, 3 ]) slog(strValue)
Decoding
jsonString = json:encode([], data) slog(jsonEncode([], [name: "Bob", age: 25] ))
### Testing With JSON Schema Validator
One designed use case:
```muu
schema = [
type: "object",
properties: [ name: [ type: "string" ], age: [ type: "integer" ] ]
]
person = { "name": "Alice", "age": 30 }
result = json:schema(schema, person)
slog(result)
// => { ok: true, errors: [ ] } if passes
Technical
-
Supports two output types for conversion: Lava <!> JSON and back.
-
In Lava scripts:
-
JSON:decode-- loads string JSON content, returns Lava Value -
JSON:encode-- encodes Lava Value, returns JSON String -
Partial and __support handling for all functions with the same placeholder/partial return values as any Lava bridge.
A Lava JSON plugin adds strong functionality, fulfy partial usage, and proper validation according to your doc syntax.
Contributing
- Reports and pull requests are welcomed at https://gitlab.com/tofo/mumu-json
- Feel free to raise issues, submit patches, and share improvements.