# serializeHeapJson

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

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

Capture a heap as a [SerializedHeap](/reference/knotkit/heap/wire/type-aliases/serializedheap/). O(n) time and space — a copy of the internal node array plus the discriminants, nothing more.

The snapshot is faithful, not canonical: it records the exact internal layout, never the order function (functions do not serialize — restoring re-supplies it, see [deserializeHeapJson](/reference/knotkit/heap/wire/functions/deserializeheapjson/)), and never a reheapified equivalent. The nodes are copied defensively, so later mutation of either the heap or the payload cannot reach the other.

## Type Parameters

### A

`A`

## Parameters

### heap

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

## Returns

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

## Throws

With code `FOREIGN_STRUCTURE` when `heap` was not produced by this package’s heap factories — including a priority queue passed by mistake, which carries the package’s state bridge but not a heap `kind`.
