lint · API

mojolint.json

A small JSON reader and writer — enough for JSON-RPC.

Parsed documents are arenas: every value is a JsonNode in JsonDoc.nodes, children are indices, and the root is node 0. That keeps the type flat (no recursive struct) and makes lookups cheap: doc.get(node, "key"), doc.at(node, i), doc.string(node), doc.int(node). A missing key or a type mismatch gives -1 / an empty value rather than raising, so a chain of lookups over a reply that may not have the shape we hoped for reads straight.

Structs

JsonNode

struct JsonNode

Implements Copyable, Movable

Fields
kind: UInt8
text: String: The decoded string, the number's spelling, or true/false.
keys: List[String]: Object keys, parallel to children; empty for arrays.
children: List[Int]: Indices of array items or object values, in document order.

JsonDoc

struct JsonDoc

Implements Movable

Fields
nodes: List[JsonNode]

__init__

def __init__(out self)

kind

def kind(self, node: Int) -> UInt8

get

def get(self, node: Int, key: String) -> Int

The value of key in object node, or -1.

at

def at(self, node: Int, index: Int) -> Int

Item index of array node, or -1.

count

def count(self, node: Int) -> Int

Number of items in an array or members in an object; 0 otherwise.

string

def string(self, node: Int) -> String

The text of a string node; empty for anything else.

int

def int(self, node: Int, default: Int = Int(-1)) -> Int

The value of an integer number node, else default.

is_null

def is_null(self, node: Int) -> Bool

Functions

parse_json

def parse_json(text: String) -> JsonDoc

Parse one JSON value; the root is node 0.

json_quote

def json_quote(text: String) -> String

text as a JSON string literal, quotes included.

Aliases

comptime NULL = UInt8(0)
comptime BOOL = UInt8(1)
comptime NUMBER = UInt8(2)
comptime STRING = UInt8(3)
comptime ARRAY = UInt8(4)
comptime OBJECT = UInt8(5)