Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

JSONZuri
NullNil
StringString
NumberNumber
BooleanBoolean
ArrayList
ObjectDict

Zuri to JSON object mapping

ZuriJSON
nilNull
IntegerNumber
NumberNumber
CharString
StringString
ListArray
DictObject
Instance of class implementing to_json() decoratorAny

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.

NameKindSummary
json.decodefunctionDecodes the input JSON string into Zuri objects
json.dumpfunctionDumps the given value into a json file at the specified path.
json.encodefunctionJSON encodes the given value with a recursive depth up to max_depth.
json.parsefunctionParses 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 is true.
  • 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 decode
  • allow_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 is true.
  • 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