# avro.json

> A module of avro's API.

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`

```mojo
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`

```mojo
def scalar(kind: Int) -> Self
```

### `JsonDoc`

```mojo
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__`

```mojo
def __init__(out self)
```

#### `kind`

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

#### `is_null`

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

#### `as_bool`

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

#### `as_int`

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

#### `as_float`

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

#### `as_string`

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

#### `len_of`

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

#### `child`

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

#### `key_at`

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

#### `get`

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

Object member lookup by name; -1 when absent.

#### `has`

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

#### `write_node`

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

#### `to_json`

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

#### `write_to`

```mojo
def write_to(self, mut writer: T)
```

## Functions

### `write_json_string`

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

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

### `parse_json`

```mojo
def parse_json(text: StringSpan) -> JsonDoc
```

Parse `text` into a `JsonDoc`.

## Aliases

### `JSON_NULL`

```mojo
comptime JSON_NULL = Int(0)
```

### `JSON_BOOL`

```mojo
comptime JSON_BOOL = Int(1)
```

### `JSON_INT`

```mojo
comptime JSON_INT = Int(2)
```

### `JSON_FLOAT`

```mojo
comptime JSON_FLOAT = Int(3)
```

### `JSON_STRING`

```mojo
comptime JSON_STRING = Int(4)
```

### `JSON_ARRAY`

```mojo
comptime JSON_ARRAY = Int(5)
```

### `JSON_OBJECT`

```mojo
comptime JSON_OBJECT = Int(6)
```
