json
import json
Provides APIs for encoding and decoding JSON data.
JavaScript Object Notation (JSON) is a lightweight, text-based, language-independent data interchange format. It was derived from the ECMAScript Programming Language Standard. JSON defines a small set of formatting rules for the portable representation of structured data.
This implementation complies with RFC 8259.
JSON to Zuri value mapping
| JSON | Zuri |
|---|---|
| Null | Nil |
| String | String |
| Number | Number |
| Boolean | Boolean |
| Array | List |
| Object | Dict |
Zuri to JSON object mapping
| Zuri | JSON |
|---|---|
nil | Null |
| Integer | Number |
| Number | Number |
| Char | String |
| String | String |
| List | Array |
| Dict | Object |
Instance of class implementing to_json() decorator | Any |
Example,
%> import json
%> json.encode([1, 2, 3])
'[1,2,3]'
%>
%> json.encode({name: 'Zuri', version: '0.1.0'})
'{"name":"Zuri","version":"0.1.0"}'
%>
%> json.encode({name: 'Zuri', version: '0.1.0'}, false)
'{
"name": "Zuri",
"version": "0.1.0"
}'
The json API
Every public name in json, wherever it is declared. Each links to the
page that documents it.
| Name | Kind | Summary |
|---|---|---|
json.decode | function | Decodes the input JSON string into Zuri objects |
json.dump | function | Dumps the given value into a json file at the specified path. |
json.encode | function | JSON encodes the given value with a recursive depth up to max_depth. |
json.parse | function | Parses a file containing json data. |
Functions
encode()
json.encode(value, compact, max_depth) -> string
JSON encodes the given value with a recursive depth up to max_depth.
If compact is true, the resulting json string will be tightly
packed. i.e. spaces will be trimmed from objects and arrays. Otherwise,
the JSON output will be pretty formatted.
Parameters
value(any)compact(?bool) — Default value istrue.max_depth(?number) — is the maximum recursive depth for encoding, default = 1024.
Returns string
Raises Error: If the value cannot be encoded to json.
Note: pretty formatting use 2 spaces instead of tabs.
decode()
json.decode(value, allow_comments)
Decodes the input JSON string into Zuri objects
Parameters
value(string) — The string to decodeallow_comments(?bool) — Can be set to enable/disable C-style comments in json [default = true]
Returns — object
Raises Error: If the json is invalid.
parse()
json.parse(path, allow_comments)
Parses a file containing json data.
Parameters
path(string)allow_comments(?bool) — Can be set to enable/disable C-style comments in json [default = true]
Returns — object
Raises Error: If the file cannot be read or if the json is
invalid.
dump()
json.dump(value, path, compact, max_depth)
Dumps the given value into a json file at the specified path.
Parameters
value(any)path(string)compact(?bool) — Default value istrue.max_depth(?number) — is the maximum recursive depth for encoding, default = 1024.
Raises Error: If the file cannot be written to.
Note: pretty formatting use 2 spaces instead of tabs.
2021, Richard Ore and Zuri contributors