avro · API

avro.json

A small, self-contained JSON reader/writer.

Avro schemas are JSON, and Object Container File metadata carries raw JSON strings (Iceberg puts its table schema and partition spec there), so avro.mojo needs a JSON parser. It is deliberately in-repo: the library then has no imports outside the Mojo standard library, and a consumer only needs -I ../avro.mojo/src.

The document is an arena (List[JsonNode]) with integer child indices — Mojo structs cannot contain themselves, and an arena also makes the recursive schema references Avro allows fall out naturally.

Structs

JsonNode

struct JsonNode

One JSON value inside a JsonDoc arena.

Implements Copyable, Movable

Fields
kind: Int
b: Bool
i: Int64
f: Float64
s: String: String payload for JSON_STRING; unused otherwise.
keys: List[String]: Object member names, in document order. Empty for non-objects.
kids: List[Int]: Child node indices: array elements, or object member values.

scalar

def scalar(kind: Int) -> Self

JsonDoc

struct JsonDoc

A parsed JSON document: a flat arena plus the index of the root value.

Implements Copyable, Movable, Writable

Fields
nodes: List[JsonNode]
root: Int

__init__

def __init__(out self)

kind

def kind(self, i: Int) -> Int

is_null

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

as_bool

def as_bool(self, i: Int) -> Bool

as_int

def as_int(self, i: Int) -> Int64

as_float

def as_float(self, i: Int) -> Float64

as_string

def as_string(self, i: Int) -> String

len_of

def len_of(self, i: Int) -> Int

child

def child(self, i: Int, k: Int) -> Int

key_at

def key_at(self, i: Int, k: Int) -> String

get

def get(self, i: Int, name: StringSpan) -> Int

Object member lookup by name; -1 when absent.

has

def has(self, i: Int, name: StringSpan) -> Bool

write_node

def write_node(self, i: Int, mut out: String)

to_json

def to_json(self, i: Int) -> String

write_to

def write_to(self, mut writer: T)

Functions

write_json_string

def write_json_string(s: StringSpan, mut out: String)

Append s to out as a quoted, escaped JSON string.

parse_json

def parse_json(text: StringSpan) -> JsonDoc

Parse text into a JsonDoc.

Aliases

comptime JSON_NULL = Int(0)
comptime JSON_BOOL = Int(1)
comptime JSON_INT = Int(2)
comptime JSON_FLOAT = Int(3)
comptime JSON_STRING = Int(4)
comptime JSON_ARRAY = Int(5)
comptime JSON_OBJECT = Int(6)