json Library Reference¶
The json module¶
Constants¶
- $null Constant¶
Is what “null” parses to.
Conditions¶
- <json-error> Open Instantiable Class¶
All JSON errors are subclasses of this class.
- Superclasses:
<format-string-condition>
<error>
- <json-parse-error> Instantiable Class¶
Any error signalled during parsing (except for file system errors) will be an instance of this.
- Superclasses:
parse-json
¶
- parse-json Open Generic function¶
Parse json formatted text from the given source. This is the main user-visible entry point for parsing. table-class, if provided, should be a subclass of
<table>
to use when creating a json “object”.- Signature:
parse-json (source, #key strict?, table-class) => (json)
- Parameters:
- Values:
json – A JSON
<object>
- Discussion:
The parse is strict by default. If
strict?:
#f
is used then:# is allowed as a comment character
”<c>” is equivalent to
<c>
, where <c> is not a defined escape character.trailing commas are allowed in arrays and objects
- parse-json(<string>) Method¶
Parse a JSON object from a
<string>
.- Signature:
parse-json (source, #key strict?, table-class) => (json)
- Parameters:
- Values:
json – An instance of
<object>
.
- Example:
let data = """{"a": 1, "b": 2,}"""; let parsed = parse-json(data, strict?: #f); let a = parsed["a"];
Run this example in https://play.opendylan.org
Note the use of
strict?: #f
is needed since data has a trailing comma after the number 2.
- parse-json(<stream>) Method¶
Parse a JSON object from a
<stream>
.- Signature:
parse-json (source, #key strict?, table-class) => (json)
- Parameters:
- Values:
json – An instance of
<object>
.
- Example:
with-open-file (fs = "data.json") let data = parse-json(fs, strict?: #f); ... end;
Run an example with a string stream in https://play.opendylan.org
print-json
¶
- print-json Function¶
Print an object in JSON format.
- Signature:
print-json (object, stream, #key indent, sort-keys?) => ()
- Parameters:
- Discussion:
If indent is false, object is printed with minimal whitespace. If an integer, then use pretty printing and output indent spaces for each indent level.
If sort-keys?: is true, output object keys in lexicographical order.
do-print-json
¶
Override this to print your own objects in JSON format. It can be implemented by converting objects to built-in Dylan types (tables, collections, etc) and calling print-json on those objects, or by writing json syntax directly to stream.
If indent: was passed to print then stream will be a pretty printing stream and the io:pprint module may be used to implement pretty printing.
- do-print-json Open Generic function¶
- Signature:
do-print-json (object, stream) => ()
- Parameters:
object – An instance of
<object>
.stream – An instance of
<stream>
.
- do-print-json($null) Method¶
- Parameters:
object – $null
stream – An instance of
<stream>
.
- do-print-json(<integer>) Method¶
Print an
<integer>
in JSON format.- Parameters:
object – An instance of
<integer>
.stream – An instance of
<stream>
.
- do-print-json(<float>) Method¶
Print a
<float>
in JSON format.- Parameters:
object – An instance of
<float>
.stream – An instance of
<stream>
.
- do-print-json(<boolean>) Method¶
Print a
<boolean>
in JSON format.- Parameters:
object – An instance of
<boolean>
.stream – An instance of
<stream>
.
- do-print-json(<string>) Method¶
Print a
<string>
in JSON format.- Parameters:
object – An instance of
<string>
.stream – An instance of
<stream>
.
- do-print-json(<collection>) Method¶
Print a
<collection>
in JSON format.- Parameters:
object – An instance of
<collection>
.stream – An instance of
<stream>
.