In development. It changes without notice.
Topic
JSON Reader, Writer and Binder
JSON read in pieces, written to a sink and bound to a record by a table, with no allocation.
The reader takes valid UTF-8 only, bounds how deep and how long a text may be, and names each refusal with its place. The binder fills a record from an object through a table that the compiler checks against the record, and a member that is a secret wipes itself. The writer clears the buffer it hands on.
Every portal route reads its body through the binder and writes its answer through the writer; the backup document keeps its own reader and writer (vault/
- See also
- Utilities
Files | |
| file | src/ |
| Bind a JSON object to a staging struct through a table checked at compile time. | |
| file | src/ |
| A strict streaming JSON reader: no allocation, no copy of the text. | |
| file | src/ |
| The UTF-8 rule the reader and the writer share (RFC 3629 §4). | |
| file | src/ |
| Clear a buffer so the compiler cannot drop the store. | |
| file | src/ |
| A streaming JSON writer: values in, RFC 8259 text out, no allocation. | |
Classes | |
| struct | kleidos:: |
| A text field that wipes itself: the type a secret member binds to. More about kleidos:: | |
| struct | kleidos:: |
| What was read of one object, one bit per member of its table. More about kleidos:: | |
| struct | kleidos:: |
| One entry handed to a hook: an array entry, or an open map's member. More about kleidos:: | |
| struct | kleidos:: |
| One member a table knows. Built only by the consteval functions below. More about kleidos:: | |
| struct | kleidos:: |
| The members an object knows. Built only by the consteval functions below. More about kleidos:: | |
| struct | kleidos:: |
| The outcome of one bind(). More about kleidos:: | |
| class | kleidos:: |
| Binds the events of one document to staging structs. More about kleidos:: | |
| class | kleidos:: |
| Bind a body that arrives in pieces, such as a request read chunk by chunk. More about kleidos:: | |
| struct | kleidos:: |
| Where one string value goes as the reader unescapes it. More about kleidos:: | |
| class | kleidos:: |
| What the reader reports to, one call per token. More about kleidos:: | |
| class | kleidos:: |
| Reads one JSON value as its bytes arrive. More about kleidos:: | |
| struct | kleidos:: |
| What a lead byte promises about the bytes after it. More about kleidos:: | |
| class | kleidos:: |
| Streams JSON text to a sink, value by value. More about kleidos:: | |
Macros | |
| #define | KLEIDOS_ |
A text member: field is a char[N] of Type; overflow is a kleidos:: | |
| #define | KLEIDOS_ |
| A secret member: field is a SecretText<N>; overflow refuses. | |
| #define | KLEIDOS_ |
| A signed integer member in [min, max]. | |
| #define | KLEIDOS_ |
| An unsigned integer member in [min, max]. | |
| #define | KLEIDOS_ |
| A boolean member. | |
| #define | KLEIDOS_ |
A nested object member, described by the table child. | |
| #define | KLEIDOS_ |
An array of objects: each entry is read into field (wiped first), then hook runs. | |
| #define | KLEIDOS_ |
An array of strings: each entry is read into field, then hook runs. | |
| #define | KLEIDOS_ |
An array of integers in [min, max], each handed to hook. | |
| #define | KLEIDOS_ |
| An open map for Type: any key, values an integer in [min, max] or a boolean. | |
| #define | KLEIDOS_ |
The table of Type from the row array members. | |
| #define | KLEIDOS_ |
| The same, with what the object held written to Type:: | |
Typedefs | |
| using | kleidos:: |
| using | kleidos:: |
| using | kleidos:: |
| Where the writer's text goes. Returns false to stop the writer. | |
Functions | |
| const char * | kleidos:: |
| BindResult | kleidos:: |
| Bind a whole body held in one buffer. | |
| void | kleidos:: |
| template<typename T, typename F> | |
| consteval Member | kleidos:: |
| A text row: a char[N] field. Use KLEIDOS_ | |
| template<typename T, typename F> | |
| consteval Member | kleidos:: |
| A secret row: a SecretText<N> field, never clamped. Use KLEIDOS_ | |
| template<typename T, typename F> | |
| consteval Member | kleidos:: |
| An integer row. Use KLEIDOS_ | |
| template<typename T, typename F> | |
| consteval Member | kleidos:: |
| A boolean row. Use KLEIDOS_ | |
| template<typename T, typename F> | |
| consteval Member | kleidos:: |
| A nested-object row. Use KLEIDOS_ | |
| template<typename T, typename F> | |
| consteval Member | kleidos:: |
| An array-of-objects row. Use KLEIDOS_ | |
| template<typename T, typename F> | |
| consteval Member | kleidos:: |
| An array-of-strings row. Use KLEIDOS_ | |
| template<typename T> | |
| consteval Member | kleidos:: |
| An array-of-integers row: nothing is stored. Use KLEIDOS_ | |
| template<typename T, size_t N> | |
| consteval ObjectSpec | kleidos:: |
| A table: the rows of one struct, checked together. | |
| template<typename T, typename F> | |
| consteval size_t | kleidos:: |
| The offset of a Bound field, checked. Use KLEIDOS_ | |
| template<typename T> | |
| consteval ObjectSpec | kleidos:: |
An open map: any key, each member an integer in [min, max] or a boolean, handed to entry. Use KLEIDOS_ | |
| const char * | kleidos:: |
| constexpr Utf8Lead | kleidos:: |
| Classify a byte at or above 0x80 that starts a sequence. | |
| void | kleidos:: |
Overwrite bytes bytes at data with zero, unconditionally. | |
| bool | kleidos:: |
Whether len bytes at text are valid UTF-8 with no NUL. | |
| size_t | kleidos:: |
Bytes of the valid UTF-8 sequence at text, under the same rule. | |
Variables | |
| constexpr size_t | kleidos:: |
| Tables hold at most this many members: one bit each in a Bound. | |
| constexpr size_t | kleidos:: |
| constexpr uint16_t | kleidos:: |
| ObjectSpec:: | |
| template<typename T> | |
| constexpr Member | kleidos:: |
| constexpr size_t | kleidos:: |
| Deepest object/array nesting the reader accepts. | |
| constexpr size_t | kleidos:: |
| Bytes a delivered key may take, the terminator included. | |
| constexpr uint32_t | kleidos:: |
| Most digits an integer may carry: 18 always fit an int64_t. | |
| constexpr size_t | kleidos:: |
| A reader limit that is no limit. | |
| constexpr uint8_t | kleidos:: |
| Lowest continuation byte after the first. | |
| constexpr uint8_t | kleidos:: |
| Highest continuation byte. | |
Typedef Documentation
ElementFn #
An array of objects' entry closed.
- Parameters
-
Name Direction Meaning user in The bind's user pointer. element in The element struct. bound in What it held.
- Returns
- False to refuse the document.
EntryFn #
An array entry or a map member arrived.
- Parameters
-
Name Direction Meaning user in The bind's user pointer. entry in The entry.
- Returns
- False to refuse the document.
Enumeration Type Documentation
BindError #
Why a bind stopped, beyond the reader's own verdict.
| Enumerator | |
|---|---|
| None | No failure. |
| Read | The reader refused the text; see BindResult:: |
| Shape | A value of the wrong JSON type, or a root that is not an object. |
| Range | An integer outside its row's range. |
| Number | A fraction, an exponent or too many digits where an integer belongs. |
| Duplicate | A key that arrived twice. |
| Unknown | A key the table does not know, in a table that refuses those. |
| TooMany | More array entries or map members than the row allows. |
| Refused | A hook refused an element or an entry. |
Error #
Why a read stopped.
Kind #
What a member's value is and where it goes.
| Enumerator | |
|---|---|
| Text | A string into a char[N] or a SecretText<N>. |
| Int | An integer in [min, max] into a signed field. |
| UInt | An integer in [min, max] into an unsigned field. |
| Bool | true / false into a bool. |
| Object | A nested object, through its own table. |
| Objects | An array of objects: one element struct per entry, then a hook. |
| Texts | An array of strings: one text field per entry, then a hook. |
| Ints | An array of integers in [min, max], each handed to a hook. |
| Scalar | An open map's value: an integer or a boolean, handed to a hook. |
Overflow #
What a string longer than its destination does.
| Enumerator | |
|---|---|
| Refuse | The read fails with Error:: |
| Clamp | The longest head that ends on a whole code point is kept. |
| Drop | Nothing is kept: the destination reads empty. |
Unknown #
Function Documentation
bind() #
Bind a whole body held in one buffer.
On any refusal the staging struct is wiped whole, so a refused request leaves no half-bound secret behind.
- Parameters
-
Name Direction Meaning spec in The root table. staging out The struct specdescribes.body in The body. len in Its bytes. user in Passed to every hook. maxBytes in Most bytes the body may take.
- Returns
- The verdict, where it stopped, and what the root held.
bindErrorName() #
- Parameters
-
Name Direction Meaning error in A bind error.
- Returns
- Its name.
boolRow() #
A boolean row. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct. F The field's type, bool.
- Parameters
-
Name Direction Meaning key in The JSON key. offset in The field's offset in T.
- Returns
- The row.
boundField() #
errorName() #
- Parameters
-
Name Direction Meaning error in A reader error.
- Returns
- Its name, for a log line or a test message.
integerRow() #
An integer row. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct. F The field's integer type; its signedness picks Int or UInt.
- Parameters
-
Name Direction Meaning key in The JSON key. offset in The field's offset in T.min in Lowest value accepted. max in Highest value accepted.
- Returns
- The row.
intsRow() #
An array-of-integers row: nothing is stored. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct the row belongs to.
- Parameters
-
Name Direction Meaning key in The JSON key. min in Lowest value accepted. max in Highest value accepted. most in Most entries. entry in Called per entry.
- Returns
- The row.
mapSpec() #
An open map: any key, each member an integer in [min, max] or a boolean, handed to entry. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct the map binds for (it holds nothing of the map).
- Parameters
-
Name Direction Meaning min in Lowest integer value. max in Highest integer value. most in Most members. entry in The hook.
- Returns
- The table.
objectRow() #
A nested-object row. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct. F The nested struct's type.
- Parameters
-
Name Direction Meaning key in The JSON key. offset in The field's offset in T.child in The table of F.
- Returns
- The row.
objectSpec() #
A table: the rows of one struct, checked together.
Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct. N Rows.
- Parameters
-
Name Direction Meaning members in The rows, a constexpr array that outlives the table. unknown in What a key the table does not know does. boundAt in Offset of a Bound field of T, or kNoBound.
- Returns
- The table.
objectsRow() #
An array-of-objects row. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct. F The element struct's type.
- Parameters
-
Name Direction Meaning key in The JSON key. offset in The element field's offset in T.child in The table of F.most in Most entries. element in Called as each entry closes, before the element is wiped.
- Returns
- The row.
secretRow() #
A secret row: a SecretText<N> field, never clamped. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct. F The field's type, a SecretText<N>.
- Parameters
-
Name Direction Meaning key in The JSON key. offset in The field's offset in T.
- Returns
- The row.
sequenceLength() #
Bytes of the valid UTF-8 sequence at text, under the same rule.
- Parameters
-
Name Direction Meaning text in First byte; may be null only when lenis 0.len in Bytes available from text.
- Returns
- 1 to 4 for a valid sequence (an ASCII byte other than NUL is 1); 0 for a NUL, a byte that cannot start a sequence, or a sequence that is cut short or malformed.
specError() #
Not constexpr on purpose: calling it from a consteval builder stops the build at the offending row. Never defined.
- Parameters
-
Name Direction Meaning why in What is wrong with the row or table.
textRow() #
A text row: a char[N] field. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct. F The field's type, a char[N].
- Parameters
-
Name Direction Meaning key in The JSON key. offset in The field's offset in T.overflow in What a longer value does.
- Returns
- The row.
textsRow() #
An array-of-strings row. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct. F The text field's type, a char[N] each entry is read into.
- Parameters
-
Name Direction Meaning key in The JSON key. offset in The field's offset in T.overflow in What a longer entry does. most in Most entries. entry in Called per entry, before the field is wiped.
- Returns
- The row.
utf8Lead() #
Classify a byte at or above 0x80 that starts a sequence.
- Parameters
-
Name Direction Meaning byte in A byte that is not ASCII.
- Returns
- Its promise; trailing == 0 when it cannot start a sequence (a continuation byte, C0, C1 or F5..FF).
validText() #
Whether len bytes at text are valid UTF-8 with no NUL.
The rule the reader enforces on input (RFC 3629, no \u0000), so what the writer accepts is exactly what the reader takes back.
- Parameters
-
Name Direction Meaning text in First byte; may be null only when lenis 0.len in Bytes to check.
- Returns
- True when the span is valid.
wipe() #
Overwrite bytes bytes at data with zero, unconditionally.
- Parameters
-
Name Direction Meaning data out Start of the block; nullptr is a no-op. bytes in Number of bytes to clear.
Variable Documentation
kMapValue #
The one value rule of an open map. Use KLEIDOS_
- Template Parameters
-
Name Meaning T The struct the map binds for.
kMaxMapMembers #
An open map holds at most this many members: the binder keeps a 64-bit hash of each key read so far to refuse one that arrives twice.