# `LiveReact.Patch`
[🔗](https://github.com/mrdotb/live_react/blob/v2.0.0/lib/live_react/patch.ex#L1)

Encodes LiveReact patch operations into the compact wire format used by
`data-props-diff` and `data-streams-diff`.

The payload is a concatenated sequence of operations. Dynamic text fields are
JavaScript-string-length-prefixed, so paths and values can contain delimiters
without extra escaping.

Operation codes:

| Code | Operation |
| --- | --- |
| `a` | `add` |
| `d` | `remove` |
| `r` | `replace` |
| `u` | `upsert` |
| `l` | `limit` |
| `n` | nonce marker, ignored while decoding |

Normal operations use:

```text
<op><path_len>:<path><value>
```

`remove` omits `<value>`. The nonce marker uses `n<digits>` and exists only
to force LiveView to send a changed attribute.

Value tags:

| Tag | Value |
| --- | --- |
| `z` | `nil` |
| `b0`, `b1` | booleans |
| `n<len>:<number>` | number |
| `s<len>:<string>` | string |
| `J<len>:<caret-encoded JSON>` | maps, lists, and complex values |

Paths are transported as JSON Pointer strings unchanged.

# `deserialize`

Deserializes a compact patch payload into list-shaped operations.

Returns `[]` for an empty payload. Decoded operations are shaped as
`[op, path]` for `remove` and `[op, path, value]` for all value-bearing
operations. Nonce markers are skipped.

# `encode_object`

Encodes a JSON value for safe, compact HTML attribute transport.

The value is encoded with Jason's default JSON escaping, then JSON quote
characters are replaced with `^`. Literal `~` and `^` characters are escaped
as `~~` and `~^`, so the transform is reversible by `decode_object/1`.

# `serialize`

Serializes patch maps into a compact binary payload.

Expected patch shapes are `%{op: op, path: path, value: value}`,
`%{op: "remove", path: path}`, and `%{op: "test", path: "", value: nonce}`.
The nonce test operation is encoded as a marker and is not returned by
`deserialize/1`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
