# JSONReassembler
<span class="source-link">[[Source]](src/json/json_reassembler.md#L-0-17)</span>

Assembles a run of JSON tokens into `JSONValue`s.

This is the opt-in path from a token stream back to a materialized value. Plug
it into a `JSONTokenParser` — it is a `JSONTokenNotify`, so tokens flow
into it directly — or hand it tokens yourself with `add()`. Either way,
`take_values()` returns the top-level values that have completed, the same
`JSONValue` the batch `JSONParser` would build:

```pony
let re = JSONReassembler
let parser = JSONTokenParser(re)
parser.feed(chunk)?
for value in re.take_values().values() do
  // value : JSONValue — use with JSONNav, JSONLens, JSONPath, JSONPrinter
end
```

Memory is the caller's choice: building a value costs memory proportional
to that value, so hand the reassembler only the tokens you actually want
materialized and drop the rest. There is no built-in size cap on what it
builds, so do not blind-drain an untrusted stream into it.

`take_values()` returns only top-level values that have completed, and
drains them — it is not a plain accessor. If a run ends part-way through
a value, that value stays buffered — `mid_value()` reports it — and the
tokens that finish it can arrive later. `add()` expects tokens in the
order `JSONTokenParser` emits them; a run that is not a valid emission
sequence produces an unspecified result.


```pony
class ref JSONReassembler is
  JSONTokenNotify ref
```

#### Implements

* [JSONTokenNotify](json-JSONTokenNotify.md) ref

---

## Constructors

### create
<span class="source-link">[[Source]](src/json/json_reassembler.md#L-0-51)</span>


```pony
new ref create()
: JSONReassembler ref^
```

#### Returns

* [JSONReassembler](json-JSONReassembler.md) ref^

---

## Public Functions

### add
<span class="source-link">[[Source]](src/json/json_reassembler.md#L-0-55)</span>


Fold one token into the value under construction. Tokens must arrive in the
order `JSONTokenParser` emits them; a run that is not a valid emission
sequence produces an unspecified result (but never corrupts a well-formed
run that follows a `take_values()`).


```pony
fun ref add(
  token: JSONToken)
: None val
```
#### Parameters

*   token: [JSONToken](json-JSONToken.md)

#### Returns

* [None](builtin-None.md) val

---

### apply
<span class="source-link">[[Source]](src/json/json_reassembler.md#L-0-97)</span>


`JSONTokenNotify` entry point — folds the token in, ignoring `parser`.


```pony
fun ref apply(
  parser: JSONTokenParser ref,
  token: JSONToken)
: None val
```
#### Parameters

*   parser: [JSONTokenParser](json-JSONTokenParser.md) ref
*   token: [JSONToken](json-JSONToken.md)

#### Returns

* [None](builtin-None.md) val

---

### take_values
<span class="source-link">[[Source]](src/json/json_reassembler.md#L-0-103)</span>


Take the top-level values completed since the last call, and reset — a
second call returns an empty array unless more values have completed. A
value whose root container has not yet closed is not included; check
`mid_value()` for it.


```pony
fun ref take_values()
: Array[JSONValue] iso^
```

#### Returns

* [Array](builtin-Array.md)\[[JSONValue](json-JSONValue.md)\] iso^

---

### mid_value
<span class="source-link">[[Source]](src/json/json_reassembler.md#L-0-112)</span>


True while tokens have been folded for a value that has not closed yet.
This reports only the reassembler's own buffered partial; for byte-level
truncation of a stream, use `JSONTokenParser.incomplete()`.


```pony
fun box mid_value()
: Bool val
```

#### Returns

* [Bool](builtin-Bool.md) val

---

