JSON

Module json

import json

JSON as Zirric values, not as a tree of its own.

JSON maps onto Zirric’s own values: an object is a prelude.Dict with String keys, an array is an prelude.Array, and null is void. A number is an prelude.Int when it was written as one and a prelude.Float otherwise, so 1 and 1.0 stay apart.

parse and format move between text and that tree. To move between the tree and your own data types, hand this module to coding: coding.encodeWith(json, value) consults the attributes below before falling back to @coding’s.

Contents


Attributes

Decode

`json/attributes.zirr:20`
attr Decode {
	parse
}

Decodes a field from its raw JSON tree, overriding @coding.Decode.

Fields

Field Description
parse fn(raw: Any) -> Result.

Default

`json/attributes.zirr:14`
attr Default {
	value
}

The value a field takes when its JSON key is absent, overriding @coding.Default.

Fields

Field Description
value The value to use.

Encode

`json/attributes.zirr:26`
attr Encode {
	format
}

Encodes a field to a JSON tree, overriding @coding.Encode.

Fields

Field Description
format fn(value: Any) -> Result.

Ignore

`json/attributes.zirr:11`
attr Ignore

Marks a field as one that does not travel as JSON, overriding @coding.Ignore.


Name

`json/attributes.zirr:5`
attr Name {
	text
}

The JSON key a field is read and written under, overriding @coding.Name for JSON only.
coding finds these by name, so declaring them here is all it takes for them to win.

Fields

Field Description
text The key to use.

Functions

format

`json/json.zirr:11`
extern fn format(value: Any) -> Result

Renders a value as JSON text, failing with Err when it holds something JSON has no form for.
Object keys come out sorted, so the same value always produces the same text.


formatIndented

`json/json.zirr:14`
extern fn formatIndented(value: Any, indent: String) -> Result

As format, but spread over lines with each level prefixed by indent.


parse

`json/json.zirr:7`
extern fn parse(text: String) -> Result

Reads JSON text, failing with Err when the text is not one JSON value.