mx_canonical_json: Encode a value as Matrix canonical JSON

View source: R/canonical_json.R

mx_canonical_jsonR Documentation

Encode a value as Matrix canonical JSON

Description

Produces the canonical JSON byte sequence the Matrix specification requires for signed objects: object keys sorted by UTF-8 byte sequence, no insignificant whitespace, raw UTF-8 for non-ASCII strings, integers only (no floats, no exponents, no decimal places, within [-(2^53)+1, (2^53)-1]), and rejection of NaN, Inf, NA values, and NA or duplicate object keys. The output is the exact byte sequence to feed into an ed25519 signer.

Usage

mx_canonical_json(x)

Arguments

x

An R value: NULL, atomic vector, or list.

Details

R named lists become JSON objects; unnamed lists and length > 1 atomic vectors become arrays. Length-1 atomics become scalars. To force a length-1 value to encode as an array, wrap it in a single-element list(...) or in I() (AsIs values are always encoded as arrays, names dropped, mirroring jsonlite).

Value

A length-1 UTF-8 character string. The result is the exact byte sequence to write to disk or feed to an ed25519 signer; non-ASCII content is preserved as raw UTF-8 bytes (jsonlite-style \uXXXX escaping is not used).

Examples

mx_canonical_json(list(b = 2, a = 1))
# "{\"a\":1,\"b\":2}"

mx_canonical_json(list(key = "abc"))
# "{\"key\":\"abc\"}"


mx.api documentation built on Sept. 12, 2026, 1:07 a.m.