std/json
On this page
Index
- type
DecodeErr - type
Patch- method
Patch.apply_to - method
Patch.apply_opt
- method
- type
Value- method
Value.encode - method
Value.get
- method
- type
Decoder- method
Decoder.write - method
Decoder.finish
- method
- type
Token - type
TokenReader- method
TokenReader.write - method
TokenReader.finish - method
TokenReader.next - method
TokenReader.more - method
TokenReader.offset - method
TokenReader.skip - method
TokenReader.value
- method
- type
JsonWriter- method
JsonWriter.object_start - method
JsonWriter.object_end - method
JsonWriter.array_start - method
JsonWriter.array_end - method
JsonWriter.key - method
JsonWriter.str - method
JsonWriter.int - method
JsonWriter.float - method
JsonWriter.number - method
JsonWriter.bool - method
JsonWriter.null - method
JsonWriter.raw - method
JsonWriter.value - method
JsonWriter.finish
- method
- fn
encode - fn
encode_str - fn
encode_indent - fn
encode_indent_str - fn
indent - fn
compact - fn
decode - fn
decode_str - fn
to_value - fn
parse_value - fn
decoder - fn
from_value - fn
token_reader - fn
tokens - fn
json_writer
Types
DecodeErr
struct DecodeErr {
path: Str
msg: Str
} derive DisplayDisplays as “at $.user.email: expected string”.
Patch
enum Patch[T] {
Absent
Null
Value(v: T)
} derive JsonPatch.apply_to
fn (p: Patch[T]) apply_to(current: T) -> TPatch.apply_opt
fn (p: Patch[T]) apply_opt(current: T?) -> T?Value
enum Value {
Null
Bool(b: Bool)
Int(n: Int)
Num(n: Float)
Str(s: Str)
List(items: List[Value])
Object(fields: List[(Str, Value)])
Trusted(kind: Str, s: Str)
}A JSON value of any shape (decision 0299; Go’s decoding into any):
an object keeps its keys in the order of the text (each key once, at
its first position, with its last value, as decode reads objects),
and a number without a fraction or exponent that fits an Int is Int,
any other number Num.
Value.encode
fn (v: Value) encode() -> StrThe JSON text of v, as encode writes its parts (strings escaped,
numbers, objects in their order).
Value.get
fn (v: Value) get(key: Str) -> Value?The value of an object’s field key; None for a missing key or a value that is not an object.
Decoder
struct Decoder {}A streaming decoder of a sequence of JSON values (Go’s json.Decoder, decision 0309): bytes in, the values completed so far out; push style, no I/O of its own. @move_only
Decoder.write
fn (var d: Decoder) write(data: Bytes) -> List[Value] ! DecodeErrThe values the input so far completes (a number at the very end waits for what follows it).
Decoder.finish
fn (var d: Decoder) finish() -> List[Value] ! DecodeErrThe rest; a value that did not end is invalid JSON: unexpected EOF.
@moves
Token
enum Token {
ObjectStart
ObjectEnd
ArrayStart
ArrayEnd
Key(s: Str)
Str(s: Str)
Number(text: Str)
Bool(b: Bool)
Null
}One token of a JSON text (decision 0495; Go’s Decoder.Token). An object
member’s name is Key; a number keeps its text as written.
TokenReader
struct TokenReader {}A token at a time reader of a sequence of JSON values (Go’s
Decoder.Token, decision 0495): pull style over bytes already in memory
(tokens), or push style (write), no I/O of its own. Strings are
unescaped as decode does (invalid UTF-8 and lone surrogate escapes
become U+FFFD); errors are DecodeErr at $ with decode’s texts.
@move_only
TokenReader.write
fn (var r: TokenReader) write(data: Bytes) -> List[Token] ! DecodeErrThe tokens the input so far completes (a number at the very end waits for what follows it).
TokenReader.finish
fn (var r: TokenReader) finish() -> List[Token] ! DecodeErrThe rest; input that ends inside an array, an object or a token is
invalid JSON: unexpected EOF. An error found after the tokens it
returns is the next call’s (of finish or next).
TokenReader.next
fn (var r: TokenReader) next() -> Token? ! DecodeErrThe next token; None at the end of the input (or, in push style, when the next token is not complete yet).
TokenReader.more
fn (var r: TokenReader) more() -> BoolGo’s Decoder.More: another element or member follows in the current array or object (false at the end of the input).
TokenReader.offset
fn (r: TokenReader) offset() -> IntGo’s Decoder.InputOffset: the byte offset after the last token.
TokenReader.skip
fn (var r: TokenReader) skip() -> () ! DecodeErrSkips the value that starts at the next token: a scalar, or an array or object through its end.
TokenReader.value
fn (var r: TokenReader) value() -> Value ! DecodeErrThe value that starts at the next token, as parse_value reads it.
JsonWriter
struct JsonWriter {}A writer of one JSON text a token at a time (decision 0495), in
encoding/json’s forms: strings escape ", \, control characters,
U+2028 and U+2029 and invalid UTF-8 as encode does, and <, >, &
as <, >, & when escape_html; numbers are the
shortest form of Go’s floatEncoder. Commas and colons are inserted. A
call out of order (a value where a key is due, an unclosed array at
finish) faults: it is a bug in the caller.
@move_only
JsonWriter.object_start
fn (var w: JsonWriter) object_start()JsonWriter.object_end
fn (var w: JsonWriter) object_end()JsonWriter.array_start
fn (var w: JsonWriter) array_start()JsonWriter.array_end
fn (var w: JsonWriter) array_end()JsonWriter.key
fn (var w: JsonWriter) key(name: Str)JsonWriter.str
fn (var w: JsonWriter) str(s: Str)JsonWriter.int
fn (var w: JsonWriter) int(n: Int)JsonWriter.float
fn (var w: JsonWriter) float(f: Float) -> () ! DecodeErrNaN and infinities are refused (Go’s UnsupportedValueError).
JsonWriter.number
fn (var w: JsonWriter) number(text: Str) -> () ! DecodeErrA number as written, checked against the JSON grammar.
JsonWriter.bool
fn (var w: JsonWriter) bool(b: Bool)JsonWriter.null
fn (var w: JsonWriter) null()JsonWriter.raw
fn (var w: JsonWriter) raw(text: Str) -> () ! DecodeErrGo’s RawMessage: a JSON text, written compacted.
JsonWriter.value
fn (var w: JsonWriter) value(v: Value) -> () ! DecodeErrA Value, token by token (a Num that is NaN or infinite is refused).
JsonWriter.finish
fn (var w: JsonWriter) finish() -> BytesThe text written; every array and object must be closed. Values written at the top level are separated by a newline (Go’s Encoder). @moves
Functions
encode
fn encode[T: Json](v: T) -> Bytesencode_str
fn encode_str[T: Json](v: T) -> Strencode_indent
fn encode_indent[T: Json](v: T, prefix: Str = "", indent: Str = " ") -> BytesGo’s MarshalIndent: encode then indent (each element on its own
line, prefixed by prefix and one indent per level; {} and [] stay
as they are).
encode_indent_str
fn encode_indent_str[T: Json](v: T, prefix: Str = "", indent: Str = " ") -> Strindent
fn indent(text: Str, prefix: Str = "", indent: Str = " ") -> Str ! DecodeErrGo’s Indent over a JSON text: numbers and strings keep their spelling;
a text that is not JSON is the DecodeErr of parse_value.
compact
fn compact(text: Str) -> Str ! DecodeErrGo’s Compact: the text with its insignificant whitespace removed.
decode
fn decode[T: Json](data: Bytes) -> T ! DecodeErrdecode_str
fn decode_str[T: Json](s: Str) -> T ! DecodeErrto_value
fn to_value[T: Json](v: T) -> ValueThe value of v’s JSON encoding (encode): field names are its JSON
names (derive Json(case:), decision 0277).
parse_value
fn parse_value(text: Str) -> Value ! DecodeErrThe value of a JSON text; DecodeErr at $ with decode’s texts.
decoder
fn decoder(max_value: Int = 67108864) -> DecoderOne value may be at most max_value bytes.
from_value
fn from_value[T: Json](v: Value) -> T ! DecodeErrThe typed value of v, as decode reads it from v’s text.
token_reader
fn token_reader(max_token: Int = 67108864) -> TokenReaderOne token may be at most max_token bytes (a string or number waiting for the rest of its text in push style).
tokens
fn tokens(data: Bytes) -> TokenReaderA reader over a whole text: next gives its tokens; the empty text and
white space give None at once.
json_writer
fn json_writer(escape_html: Bool = true) -> JsonWriter