# mojolint.json

> A module of lint's API.

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`

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

```mojo
struct JsonDoc
```

Implements: `Movable`

Fields:

- `nodes: List[JsonNode]`

#### `__init__`

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

#### `kind`

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

#### `get`

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

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

#### `at`

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

Item `index` of array `node`, or -1.

#### `count`

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

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

#### `string`

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

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

#### `int`

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

The value of an integer number node, else `default`.

#### `is_null`

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

## Functions

### `parse_json`

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

Parse one JSON value; the root is node 0.

### `json_quote`

```mojo
def json_quote(text: String) -> String
```

`text` as a JSON string literal, quotes included.

## Aliases

### `NULL`

```mojo
comptime NULL = UInt8(0)
```

### `BOOL`

```mojo
comptime BOOL = UInt8(1)
```

### `NUMBER`

```mojo
comptime NUMBER = UInt8(2)
```

### `STRING`

```mojo
comptime STRING = UInt8(3)
```

### `ARRAY`

```mojo
comptime ARRAY = UInt8(4)
```

### `OBJECT`

```mojo
comptime OBJECT = UInt8(5)
```
