cromo The Cromo Guide
Download PDF IT

std.json

The module std.json: the text of JSON, read and written. encode and pretty give the text of a value, and decode reads a value of a type from its text: the compiler writes the conversion of each struct and enum by itself, without declarations. parse gives a Value, for JSON whose shape is not known in advance.

cromo
import std.json

struct Point {
    i64 x
    i64 y
}

i32 main() {
    Point p = json.decode<Point>("{\"x\": 1, \"y\": 2}")
    println("{}", json.encode(p))   // {"x":1,"y":2}
    ret 0
}

The attribute [[json("name")]] before a field or a case gives it another name in JSON, and [[json(skip)]] before a field leaves it out. A text that is not JSON, or that does not fit the type, is an exception whose message says where.

The guide: JSON.

Made from the documentation in the sources of the library.

#Types

#Data

cromo
interface Data

The types that can be converted to JSON and back, which the compiler recognizes without a declaration: the numbers, bool, string, Value, the lists and the fixed arrays of them, the maps from string to them, the enums, and the structs whose fields can be converted. A field that cannot be converted, as a function, is a compile error that names it.

#Value

cromo
enum Value

A value of JSON, of any shape, as parse gives it. An object keeps its members in their order, also two with the same name. A number without a point or an exponent is an Int when it fits in an i64, a UInt above it; the other numbers are a Float. The methods that read a kind of value throw an exception on a value of another kind.

cromo
json.Value v = json.parse("{\"items\": [1, 2.5], \"ok\": true}")
i64 first = v.get("items").at(0).asInt()     // 1
bool ok = v.get("ok").asBool()               // true
json.Value made = json.object()
made.set("name", json.Value.Text("Cromo"))   // {"name":"Cromo"}
  • Null: null
  • Bool(bool value): true or false
  • Int(i64 value): an integer that fits in an i64
  • UInt(u64 value): an integer above the i64, up to 2^64 - 1
  • Float(f64 value): a number with a point or an exponent, or an integer too large
  • Text(string value): a string
  • Array(list<Value> items): the elements of an array, in their order
  • Object(list<Member> members): the members of an object, in their order

#Value.isNull

cromo
bool isNull() const

Whether the value is null.

#Value.asBool

cromo
bool asBool() const

The bool of a Bool; another kind of value throws an exception.

#Value.asInt

cromo
i64 asInt() const

The integer of an Int; another kind of value throws an exception, also a Float as 2.0 and a UInt.

#Value.asFloat

cromo
f64 asFloat() const

The number of a Float, an Int or a UInt as an f64, where an integer above 2^53 can lose its last digits; another kind of value throws an exception.

#Value.asText

cromo
string asText() const

A copy of the text of a Text; another kind of value throws an exception.

#Value.len

cromo
i64 len() const

The number of elements of an array, or of members of an object; another kind of value throws an exception.

#Value.has

cromo
bool has(string name) const

Whether the value is an object with a member name; false for the other kinds of value.

#Value.get

cromo
Value get(string name) const

A copy of the value of the member name of an object; with the same name twice, the last one. A missing member throws an exception, and so does a value that is not an object.

#Value.at

cromo
Value at(i64 index) const

A copy of the element at index of an array, from 0. An index out of the array throws an exception, not a panic, and so does a value that is not an array.

#Value.keys

cromo
list<string> keys() const

The names of the members of an object, in their order, a name twice if it is there twice; another kind of value throws an exception.

#Value.set

cromo
void set(string name, move Value item)

Gives the member name of an object the value item, or adds the member at the end when it is missing; another kind of value throws an exception. The value is copied, unless it is given with move.

#Value.add

cromo
void add(move Value item)

Adds the element at the end of an array; another kind of value throws an exception. The value is copied, unless it is given with move.

#Member

cromo
struct Member

A member of an object of JSON: its name and its value.

  • string name: the name, without the quotes of JSON
  • Value value: the value of the member

#Functions

#object

cromo
Value object()

An empty object, to fill with set.

#array

cromo
Value array()

An empty array, to fill with add.

#encode

cromo
string encode<T : Data>(T value)

The text of JSON of a value, compact, without spaces: {"x":1,"y":2}. A struct becomes an object with a member for each field, in their order; a list or a fixed array an array; a map an object, with its keys in the order of their bytes; a case of an enum its name, or, when it has data, an object whose only member is the case. A float is written with a point, as 2.0, so that it is read back as a float; an infinity or a NaN throws an exception, since JSON has none.

#pretty

cromo
string pretty<T : Data>(T value)

The text of JSON of a value on several lines, indented by two spaces for each level, with a space after the : of each member; the rest as in encode.

#decode

cromo
T decode<T : Data>(string text)

A value of the type T from its text of JSON: json.decode<Point>(text). A member that is missing, or null, leaves the default value in its field, and a member without a field is ignored. A text that is not JSON throws an exception with its line and column, as parse does. A value of another kind throws one with its path, as "cannot decode JSON: at users[2].age: expected an integer, found a string"; an integer field takes only an integer in its range, not 2.0, while a float field takes also an integer.

#fromValue

cromo
T fromValue<T : Data>(Value value)

A value of the type T from a Value, with the rules of decode. A struct that needs to tell a null apart declares the field json.Value, asks isNull(), and then reads the value with fromValue.

#toValue

cromo
Value toValue<T : Data>(T value)

The Value of a value, the same that parse(encode(value)) gives: to change it, or add members to it, before writing it. An infinity or a NaN throws an exception, as in encode.

#parse

cromo
Value parse(string text)

The Value of a text of JSON, as RFC 8259 defines it: without comments, without commas after the last element, and with nothing after the value but spaces. A text that is not JSON throws an exception that says where, as "cannot read JSON: line 3, column 7: expected , or }"; so do arrays and objects nested more than 512 times.