Tin
Standard library · Json

On this page

Standard library

Json

Read and write JSON whose shape is not known in advance. For a shape you know, a typed agent reply or a struct is better.

Example

let j = parseJson(r#"{"name": "Ada", "langs": ["Tin", "C"]}"#).unwrap()
print(j.field("name").asStr())
print(j.field("langs").items().len)
print(j.with("age", Num(36.0)).stringify())
Some("Ada")
2
{"name":"Ada","langs":["Tin","C"],"age":36}

All 17 symbols

Json

type Json = Null | Bool(bool) | Num(float) | Str(string) | Arr(Json[]) | Obj(()<string, Json>[])

No summary yet; the signature is the whole description.

JsonReader

type JsonReader = {  }

Where parseJson is in its text (internal).

parseJson

fn parseJson(text: string): Result<Json, string>

Read JSON text: exactly what JSON.parse accepts (RFC 8259). Err says where it went wrong. A repeated key keeps its first place and its last value; a lone surrogate escape becomes U+FFFD.

jsonQuote

fn jsonQuote(s: string): string

A string as a JSON string literal, quotes included.

Json.stringify

fn Json.stringify(j: Json): string

Compact JSON text, the same text JavaScript's JSON.stringify gives.

Json.show

fn Json.show(j: Json): string

No summary yet; the signature is the whole description.

Json.field

fn Json.field(j: Json, name: string): Json

An object's field, or Null when there is none (or this is not an object): j.field("a").field("b").

Json.has

fn Json.has(j: Json, name: string): bool

Whether an object has this field.

Json.item

fn Json.item(j: Json, i: int): Json

An array's item i, or Null.

Json.items

fn Json.items(j: Json): Json[]

An array's items ([] for anything else).

Json.entries

fn Json.entries(j: Json): ()<string, Json>[]

An object's fields in order ([] for anything else).

Json.with

fn Json.with(j: Json, name: string, value: Json): Json

The same object with this field set (replaced in place, or added at the end).

Json.asStr

fn Json.asStr(j: Json): Option<string>

No summary yet; the signature is the whole description.

Json.asNum

fn Json.asNum(j: Json): Option<float>

No summary yet; the signature is the whole description.

Json.asBool

fn Json.asBool(j: Json): Option<bool>

No summary yet; the signature is the whole description.

Json.asInt

fn Json.asInt(j: Json): Option<int>

A number that is a whole int.

Json.isNull

fn Json.isNull(j: Json): bool

No summary yet; the signature is the whole description.

See also