# deserializeHeapJson

## Call Signature

> **deserializeHeapJson**<`A`>(`payload`, `order`): [`Heap`](/reference/knotkit/heap/type-aliases/heap/)<`A`>

Defined in: [packages/heap/src/serialization/json.ts:121](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/serialization/json.ts#L121)

Rebuild a heap from a [SerializedHeap](/reference/knotkit/heap/wire/type-aliases/serializedheap/). O(n) time: one validation pass plus one array copy — the layout is adopted exactly, never reheapified, so a restored heap drains in the same sequence as the original.

Serialization is comparator-blind: the payload carries no order, so restore re-supplies the **base order used at construction** — the direction comes from the payload’s `kind`, not from reversing the order. Round-trip fidelity holds when the re-supplied order is semantically equivalent to the original; a *different* order that happens to satisfy the payload’s layout is accepted, and the restored heap then follows the order actually supplied. Comparator identity is the caller’s responsibility in their envelope — a documented limitation, not a validated fact.

Validation covers shape, kind, arity, the full heap invariant under the direction-adjusted supplied order, and — with `options` — every element; guards run before the order is ever invoked.

### Type Parameters

#### A

`A`

### Parameters

#### payload

[`SerializedHeap`](/reference/knotkit/heap/wire/type-aliases/serializedheap/)<`A`>

A statically typed serialized heap, or decoded `unknown` paired with runtime element validation.

#### order

[`Order`](/reference/knotkit/heap/type-aliases/order/)<`A`>

The base order the heap was constructed under.

### Returns

[`Heap`](/reference/knotkit/heap/type-aliases/heap/)<`A`>

### Throws

When the payload is malformed or fails validation: not an object (`INVALID_SHAPE`), a missing or unsupported schema version (`UNSUPPORTED_SCHEMA_VERSION`), an unknown or cross-family `kind` (`UNKNOWN_KIND`), a missing or mistyped `nodes` array (`MISSING_NODES`), an unsafe or missing k-ary `arity` (`INVALID_ARITY`), an element failing `options.isElement` (`INVALID_ELEMENT`), or a parent-child pair violating the heap invariant (`HEAP_INVARIANT_VIOLATION`, naming the offending index).

## Call Signature

> **deserializeHeapJson**<`A`>(`payload`, `order`, `options`): [`Heap`](/reference/knotkit/heap/type-aliases/heap/)<`A`>

Defined in: [packages/heap/src/serialization/json.ts:122](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/serialization/json.ts#L122)

Rebuild a heap from a [SerializedHeap](/reference/knotkit/heap/wire/type-aliases/serializedheap/). O(n) time: one validation pass plus one array copy — the layout is adopted exactly, never reheapified, so a restored heap drains in the same sequence as the original.

Serialization is comparator-blind: the payload carries no order, so restore re-supplies the **base order used at construction** — the direction comes from the payload’s `kind`, not from reversing the order. Round-trip fidelity holds when the re-supplied order is semantically equivalent to the original; a *different* order that happens to satisfy the payload’s layout is accepted, and the restored heap then follows the order actually supplied. Comparator identity is the caller’s responsibility in their envelope — a documented limitation, not a validated fact.

Validation covers shape, kind, arity, the full heap invariant under the direction-adjusted supplied order, and — with `options` — every element; guards run before the order is ever invoked.

### Type Parameters

#### A

`A`

### Parameters

#### payload

`unknown`

A statically typed serialized heap, or decoded `unknown` paired with runtime element validation.

#### order

[`Order`](/reference/knotkit/heap/type-aliases/order/)<`A`>

The base order the heap was constructed under.

#### options

[`DeserializeHeapJsonOptions`](/reference/knotkit/heap/wire/interfaces/deserializeheapjsonoptions/)<`A`>

Runtime decoding options for payloads whose element type is not yet known.

### Returns

[`Heap`](/reference/knotkit/heap/type-aliases/heap/)<`A`>

### Throws

When the payload is malformed or fails validation: not an object (`INVALID_SHAPE`), a missing or unsupported schema version (`UNSUPPORTED_SCHEMA_VERSION`), an unknown or cross-family `kind` (`UNKNOWN_KIND`), a missing or mistyped `nodes` array (`MISSING_NODES`), an unsafe or missing k-ary `arity` (`INVALID_ARITY`), an element failing `options.isElement` (`INVALID_ELEMENT`), or a parent-child pair violating the heap invariant (`HEAP_INVARIANT_VIOLATION`, naming the offending index).

## Call Signature

> **deserializeHeapJson**(`payload`, `order`): [`Heap`](/reference/knotkit/heap/type-aliases/heap/)<`unknown`>

Defined in: [packages/heap/src/serialization/json.ts:127](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/heap/src/serialization/json.ts#L127)

Rebuild a heap from a [SerializedHeap](/reference/knotkit/heap/wire/type-aliases/serializedheap/). O(n) time: one validation pass plus one array copy — the layout is adopted exactly, never reheapified, so a restored heap drains in the same sequence as the original.

Serialization is comparator-blind: the payload carries no order, so restore re-supplies the **base order used at construction** — the direction comes from the payload’s `kind`, not from reversing the order. Round-trip fidelity holds when the re-supplied order is semantically equivalent to the original; a *different* order that happens to satisfy the payload’s layout is accepted, and the restored heap then follows the order actually supplied. Comparator identity is the caller’s responsibility in their envelope — a documented limitation, not a validated fact.

Validation covers shape, kind, arity, the full heap invariant under the direction-adjusted supplied order, and — with `options` — every element; guards run before the order is ever invoked.

### Parameters

#### payload

`unknown`

A statically typed serialized heap, or decoded `unknown` paired with runtime element validation.

#### order

[`Order`](/reference/knotkit/heap/type-aliases/order/)<`unknown`>

The base order the heap was constructed under.

### Returns

[`Heap`](/reference/knotkit/heap/type-aliases/heap/)<`unknown`>

### Throws

When the payload is malformed or fails validation: not an object (`INVALID_SHAPE`), a missing or unsupported schema version (`UNSUPPORTED_SCHEMA_VERSION`), an unknown or cross-family `kind` (`UNKNOWN_KIND`), a missing or mistyped `nodes` array (`MISSING_NODES`), an unsafe or missing k-ary `arity` (`INVALID_ARITY`), an element failing `options.isElement` (`INVALID_ELEMENT`), or a parent-child pair violating the heap invariant (`HEAP_INVARIANT_VIOLATION`, naming the offending index).
