Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · json

Docs

std/json

On this page

Index

Types

DecodeErr

struct DecodeErr {
  path: Str
  msg: Str
} derive Display

Displays as “at $.user.email: expected string”.

Source: std/json.xo:22

Patch

enum Patch[T] {
  Absent
  Null
  Value(v: T)
} derive Json

Source: std/json.xo:30

Patch.apply_to

fn (p: Patch[T]) apply_to(current: T) -> T

Source: std/json.xo:27

Patch.apply_opt

fn (p: Patch[T]) apply_opt(current: T?) -> T?

Source: std/json.xo:28

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.

Source: std/json.xo:41

Value.encode

fn (v: Value) encode() -> Str

The JSON text of v, as encode writes its parts (strings escaped, numbers, objects in their order).

Source: std/json.xo:64

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.

Source: std/json.xo:67

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

Source: std/json.xo:73

Decoder.write

fn (var d: Decoder) write(data: Bytes) -> List[Value] ! DecodeErr

The values the input so far completes (a number at the very end waits for what follows it).

Source: std/json.xo:79

Decoder.finish

fn (var d: Decoder) finish() -> List[Value] ! DecodeErr

The rest; a value that did not end is invalid JSON: unexpected EOF. @moves

Source: std/json.xo:82

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.

Source: std/json.xo:88

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

Source: std/json.xo:106

TokenReader.write

fn (var r: TokenReader) write(data: Bytes) -> List[Token] ! DecodeErr

The tokens the input so far completes (a number at the very end waits for what follows it).

Source: std/json.xo:116

TokenReader.finish

fn (var r: TokenReader) finish() -> List[Token] ! DecodeErr

The 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).

Source: std/json.xo:120

TokenReader.next

fn (var r: TokenReader) next() -> Token? ! DecodeErr

The next token; None at the end of the input (or, in push style, when the next token is not complete yet).

Source: std/json.xo:123

TokenReader.more

fn (var r: TokenReader) more() -> Bool

Go’s Decoder.More: another element or member follows in the current array or object (false at the end of the input).

Source: std/json.xo:126

TokenReader.offset

fn (r: TokenReader) offset() -> Int

Go’s Decoder.InputOffset: the byte offset after the last token.

Source: std/json.xo:128

TokenReader.skip

fn (var r: TokenReader) skip() -> () ! DecodeErr

Skips the value that starts at the next token: a scalar, or an array or object through its end.

Source: std/json.xo:131

TokenReader.value

fn (var r: TokenReader) value() -> Value ! DecodeErr

The value that starts at the next token, as parse_value reads it.

Source: std/json.xo:133

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

Source: std/json.xo:143

JsonWriter.object_start

fn (var w: JsonWriter) object_start()

Source: std/json.xo:146

JsonWriter.object_end

fn (var w: JsonWriter) object_end()

Source: std/json.xo:147

JsonWriter.array_start

fn (var w: JsonWriter) array_start()

Source: std/json.xo:148

JsonWriter.array_end

fn (var w: JsonWriter) array_end()

Source: std/json.xo:149

JsonWriter.key

fn (var w: JsonWriter) key(name: Str)

Source: std/json.xo:150

JsonWriter.str

fn (var w: JsonWriter) str(s: Str)

Source: std/json.xo:151

JsonWriter.int

fn (var w: JsonWriter) int(n: Int)

Source: std/json.xo:152

JsonWriter.float

fn (var w: JsonWriter) float(f: Float) -> () ! DecodeErr

NaN and infinities are refused (Go’s UnsupportedValueError).

Source: std/json.xo:154

JsonWriter.number

fn (var w: JsonWriter) number(text: Str) -> () ! DecodeErr

A number as written, checked against the JSON grammar.

Source: std/json.xo:156

JsonWriter.bool

fn (var w: JsonWriter) bool(b: Bool)

Source: std/json.xo:157

JsonWriter.null

fn (var w: JsonWriter) null()

Source: std/json.xo:158

JsonWriter.raw

fn (var w: JsonWriter) raw(text: Str) -> () ! DecodeErr

Go’s RawMessage: a JSON text, written compacted.

Source: std/json.xo:160

JsonWriter.value

fn (var w: JsonWriter) value(v: Value) -> () ! DecodeErr

A Value, token by token (a Num that is NaN or infinite is refused).

Source: std/json.xo:162

JsonWriter.finish

fn (var w: JsonWriter) finish() -> Bytes

The text written; every array and object must be closed. Values written at the top level are separated by a newline (Go’s Encoder). @moves

Source: std/json.xo:166

Functions

encode

fn encode[T: Json](v: T) -> Bytes

Source: std/json.xo:6

encode_str

fn encode_str[T: Json](v: T) -> Str

Source: std/json.xo:7

encode_indent

fn encode_indent[T: Json](v: T, prefix: Str = "", indent: Str = "  ") -> Bytes

Go’s MarshalIndent: encode then indent (each element on its own line, prefixed by prefix and one indent per level; {} and [] stay as they are).

Source: std/json.xo:11

encode_indent_str

fn encode_indent_str[T: Json](v: T, prefix: Str = "", indent: Str = "  ") -> Str

Source: std/json.xo:12

indent

fn indent(text: Str, prefix: Str = "", indent: Str = "  ") -> Str ! DecodeErr

Go’s Indent over a JSON text: numbers and strings keep their spelling; a text that is not JSON is the DecodeErr of parse_value.

Source: std/json.xo:15

compact

fn compact(text: Str) -> Str ! DecodeErr

Go’s Compact: the text with its insignificant whitespace removed.

Source: std/json.xo:17

decode

fn decode[T: Json](data: Bytes) -> T ! DecodeErr

Source: std/json.xo:18

decode_str

fn decode_str[T: Json](s: Str) -> T ! DecodeErr

Source: std/json.xo:19

to_value

fn to_value[T: Json](v: T) -> Value

The value of v’s JSON encoding (encode): field names are its JSON names (derive Json(case:), decision 0277).

Source: std/json.xo:59

parse_value

fn parse_value(text: Str) -> Value ! DecodeErr

The value of a JSON text; DecodeErr at $ with decode’s texts.

Source: std/json.xo:61

decoder

fn decoder(max_value: Int = 67108864) -> Decoder

One value may be at most max_value bytes.

Source: std/json.xo:76

from_value

fn from_value[T: Json](v: Value) -> T ! DecodeErr

The typed value of v, as decode reads it from v’s text.

Source: std/json.xo:84

token_reader

fn token_reader(max_token: Int = 67108864) -> TokenReader

One token may be at most max_token bytes (a string or number waiting for the rest of its text in push style).

Source: std/json.xo:110

tokens

fn tokens(data: Bytes) -> TokenReader

A reader over a whole text: next gives its tokens; the empty text and white space give None at once.

Source: std/json.xo:113

json_writer

fn json_writer(escape_html: Bool = true) -> JsonWriter

Source: std/json.xo:145

↑ index