Kleidos API Search

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/json_lite.h).

See also
Utilities

Files

file src/json/json_bind.h
Bind a JSON object to a staging struct through a table checked at compile time.
file src/json/json_reader.h
A strict streaming JSON reader: no allocation, no copy of the text.
file src/json/json_utf8.h
The UTF-8 rule the reader and the writer share (RFC 3629 §4).
file src/json/json_wipe.h
Clear a buffer so the compiler cannot drop the store.
file src/json/json_writer.h
A streaming JSON writer: values in, RFC 8259 text out, no allocation.

Classes

struct kleidos::json::SecretText< N >
A text field that wipes itself: the type a secret member binds to. More about kleidos::json::SecretText< N >
struct kleidos::json::Bound
What was read of one object, one bit per member of its table. More about kleidos::json::Bound
struct kleidos::json::Entry
One entry handed to a hook: an array entry, or an open map's member. More about kleidos::json::Entry
struct kleidos::json::Member
One member a table knows. Built only by the consteval functions below. More about kleidos::json::Member
struct kleidos::json::ObjectSpec
The members an object knows. Built only by the consteval functions below. More about kleidos::json::ObjectSpec
struct kleidos::json::BindResult
The outcome of one bind(). More about kleidos::json::BindResult
class kleidos::json::Binder
Binds the events of one document to staging structs. More about kleidos::json::Binder
class kleidos::json::BindStream
Bind a body that arrives in pieces, such as a request read chunk by chunk. More about kleidos::json::BindStream
struct kleidos::json::StringTarget
Where one string value goes as the reader unescapes it. More about kleidos::json::StringTarget
class kleidos::json::EventSink
What the reader reports to, one call per token. More about kleidos::json::EventSink
class kleidos::json::Reader
Reads one JSON value as its bytes arrive. More about kleidos::json::Reader
struct kleidos::json::Utf8Lead
What a lead byte promises about the bytes after it. More about kleidos::json::Utf8Lead
class kleidos::json::Writer
Streams JSON text to a sink, value by value. More about kleidos::json::Writer

Macros

#defineKLEIDOS_JSON_TEXT(Type, field, key, overflow)
A text member: field is a char[N] of Type; overflow is a kleidos::json::Overflow.
#defineKLEIDOS_JSON_SECRET(Type, field, key)
A secret member: field is a SecretText<N>; overflow refuses.
#defineKLEIDOS_JSON_INT(Type, field, key, min, max)
A signed integer member in [min, max].
#defineKLEIDOS_JSON_UINT(Type, field, key, min, max)
An unsigned integer member in [min, max].
#defineKLEIDOS_JSON_BOOL(Type, field, key)
A boolean member.
#defineKLEIDOS_JSON_OBJECT(Type, field, key, child)
A nested object member, described by the table child.
#defineKLEIDOS_JSON_OBJECTS(Type, field, key, child, most, hook)
An array of objects: each entry is read into field (wiped first), then hook runs.
#defineKLEIDOS_JSON_TEXTS(Type, field, key, overflow, most, hook)
An array of strings: each entry is read into field, then hook runs.
#defineKLEIDOS_JSON_INTS(Type, key, min, max, most, hook)
An array of integers in [min, max], each handed to hook.
#defineKLEIDOS_JSON_MAP(Type, min, max, most, hook)
An open map for Type: any key, values an integer in [min, max] or a boolean.
#defineKLEIDOS_JSON_SPEC(Type, members, unknown)
The table of Type from the row array members.
#defineKLEIDOS_JSON_SPEC_BOUND(Type, members, unknown, boundMember)
The same, with what the object held written to Type::boundField as it closes.

Typedefs

usingkleidos::json::ElementFn
usingkleidos::json::EntryFn
usingkleidos::json::Sink
Where the writer's text goes. Returns false to stop the writer.

Enumerations

enum class kleidos::json::Kind : uint8_t { kleidos::json::Kind::Text, kleidos::json::Kind::Int, kleidos::json::Kind::UInt, kleidos::json::Kind::Bool, kleidos::json::Kind::Object, kleidos::json::Kind::Objects, kleidos::json::Kind::Texts, kleidos::json::Kind::Ints, kleidos::json::Kind::Scalar }
What a member's value is and where it goes. More about kleidos::json::Kind
enum class kleidos::json::Unknown : uint8_t { kleidos::json::Unknown::Skip , kleidos::json::Unknown::Refuse }
What a table does with a key it does not know. More about kleidos::json::Unknown
enum class kleidos::json::BindError : uint8_t { kleidos::json::BindError::None, kleidos::json::BindError::Read, kleidos::json::BindError::Shape, kleidos::json::BindError::Range, kleidos::json::BindError::Number, kleidos::json::BindError::Duplicate, kleidos::json::BindError::Unknown, kleidos::json::BindError::TooMany, kleidos::json::BindError::Refused }
Why a bind stopped, beyond the reader's own verdict. More about kleidos::json::BindError
enum class kleidos::json::Error : uint8_t { kleidos::json::Error::None, kleidos::json::Error::Syntax, kleidos::json::Error::Depth, kleidos::json::Error::Utf8, kleidos::json::Error::Control, kleidos::json::Error::Escape, kleidos::json::Error::Nul, kleidos::json::Error::TooLong, kleidos::json::Error::TooLarge, kleidos::json::Error::Truncated, kleidos::json::Error::Refused, kleidos::json::Error::Misuse }
Why a read stopped. More about kleidos::json::Error
enum class kleidos::json::Overflow : uint8_t { kleidos::json::Overflow::Refuse , kleidos::json::Overflow::Clamp , kleidos::json::Overflow::Drop }
What a string longer than its destination does. More about kleidos::json::Overflow

Functions

const char *kleidos::json::bindErrorName (BindError error)
BindResultkleidos::json::bind (const ObjectSpec &spec, void *staging, const char *body, size_t len, void *user=nullptr, size_t maxBytes=kNoLimit)
Bind a whole body held in one buffer.
voidkleidos::json::specError (const char *why)
template<typename T, typename F>
consteval Memberkleidos::json::textRow (const char *key, size_t offset, Overflow overflow)
A text row: a char[N] field. Use KLEIDOS_JSON_TEXT.
template<typename T, typename F>
consteval Memberkleidos::json::secretRow (const char *key, size_t offset)
A secret row: a SecretText<N> field, never clamped. Use KLEIDOS_JSON_SECRET.
template<typename T, typename F>
consteval Memberkleidos::json::integerRow (const char *key, size_t offset, int64_t min, int64_t max)
An integer row. Use KLEIDOS_JSON_INT or KLEIDOS_JSON_UINT.
template<typename T, typename F>
consteval Memberkleidos::json::boolRow (const char *key, size_t offset)
A boolean row. Use KLEIDOS_JSON_BOOL.
template<typename T, typename F>
consteval Memberkleidos::json::objectRow (const char *key, size_t offset, const ObjectSpec &child)
A nested-object row. Use KLEIDOS_JSON_OBJECT.
template<typename T, typename F>
consteval Memberkleidos::json::objectsRow (const char *key, size_t offset, const ObjectSpec &child, uint32_t most, ElementFn element)
An array-of-objects row. Use KLEIDOS_JSON_OBJECTS.
template<typename T, typename F>
consteval Memberkleidos::json::textsRow (const char *key, size_t offset, Overflow overflow, uint32_t most, EntryFn entry)
An array-of-strings row. Use KLEIDOS_JSON_TEXTS.
template<typename T>
consteval Memberkleidos::json::intsRow (const char *key, int64_t min, int64_t max, uint32_t most, EntryFn entry)
An array-of-integers row: nothing is stored. Use KLEIDOS_JSON_INTS.
template<typename T, size_t N>
consteval ObjectSpeckleidos::json::objectSpec (const Member(&members)[N], Unknown unknown, size_t boundAt=kNoBound)
A table: the rows of one struct, checked together.
template<typename T, typename F>
consteval size_tkleidos::json::boundField (size_t offset)
The offset of a Bound field, checked. Use KLEIDOS_JSON_SPEC_BOUND.
template<typename T>
consteval ObjectSpeckleidos::json::mapSpec (int64_t min, int64_t max, uint32_t most, EntryFn entry)
An open map: any key, each member an integer in [min, max] or a boolean, handed to entry. Use KLEIDOS_JSON_MAP.
const char *kleidos::json::errorName (Error error)
constexpr Utf8Leadkleidos::json::utf8Lead (uint8_t byte)
Classify a byte at or above 0x80 that starts a sequence.
voidkleidos::json::wipe (void *data, size_t bytes)
Overwrite bytes bytes at data with zero, unconditionally.
boolkleidos::json::validText (const char *text, size_t len)
Whether len bytes at text are valid UTF-8 with no NUL.
size_tkleidos::json::sequenceLength (const char *text, size_t len)
Bytes of the valid UTF-8 sequence at text, under the same rule.

Variables

constexpr size_tkleidos::json::kMaxMembers
Tables hold at most this many members: one bit each in a Bound.
constexpr size_tkleidos::json::kMaxMapMembers
constexpr uint16_tkleidos::json::kNoBound
ObjectSpec::boundAt when the struct keeps no Bound.
template<typename T>
constexpr Memberkleidos::json::kMapValue
constexpr size_tkleidos::json::kMaxDepth
Deepest object/array nesting the reader accepts.
constexpr size_tkleidos::json::kMaxKeyBytes
Bytes a delivered key may take, the terminator included.
constexpr uint32_tkleidos::json::kMaxIntegerDigits
Most digits an integer may carry: 18 always fit an int64_t.
constexpr size_tkleidos::json::kNoLimit
A reader limit that is no limit.
constexpr uint8_tkleidos::json::kUtf8ContinuationLo
Lowest continuation byte after the first.
constexpr uint8_tkleidos::json::kUtf8ContinuationHi
Highest continuation byte.

Back to the top

Typedef Documentation

ElementFn #

using kleidos::json::ElementFn

An array of objects' entry closed.

Parameters
NameDirectionMeaning
userinThe bind's user pointer.
elementinThe element struct.
boundinWhat it held.
Returns
False to refuse the document.

EntryFn #

using kleidos::json::EntryFn

An array entry or a map member arrived.

Parameters
NameDirectionMeaning
userinThe bind's user pointer.
entryinThe entry.
Returns
False to refuse the document.

Back to the top

Enumeration Type Documentation

BindError #

enum class kleidos::json::BindError : uint8_t

Why a bind stopped, beyond the reader's own verdict.

Enumerator
None 

No failure.

Read 

The reader refused the text; see BindResult::read.

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 #

enum class kleidos::json::Error : uint8_t

Why a read stopped.

Enumerator
None 

No failure.

Syntax 

A byte the grammar does not allow where it stands.

Depth 

Nesting deeper than kMaxDepth.

Utf8 

A string or key that is not valid UTF-8.

Control 

A raw byte under 0x20 inside a string or key.

Escape 

An unknown escape, a bad \u, or a lone surrogate.

Nul 

\u0000: a NUL would end the field short of its text.

TooLong 

A string longer than a destination that refuses overflow.

TooLarge 

More bytes than the reader's limit.

Truncated 

The input ended before the value closed.

Refused 

The sink refused an event.

Misuse 

Fed a null pointer with a nonzero length.

Kind #

enum class kleidos::json::Kind : uint8_t

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 #

enum class kleidos::json::Overflow : uint8_t

What a string longer than its destination does.

Enumerator
Refuse 

The read fails with Error::TooLong. For anything secret.

Clamp 

The longest head that ends on a whole code point is kept.

Drop 

Nothing is kept: the destination reads empty.

Unknown #

enum class kleidos::json::Unknown : uint8_t

What a table does with a key it does not know.

Enumerator
Skip 

Skip its value, whatever it is.

Refuse 

Refuse the document: a page and a firmware that disagree.

Back to the top

Function Documentation

bind() #

BindResult kleidos::json::bind(const ObjectSpec & spec, void * staging, const char * body, size_t len, void * user = nullptr, size_t maxBytes = kNoLimit)

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
NameDirectionMeaning
specinThe root table.
stagingoutThe struct spec describes.
bodyinThe body.
leninIts bytes.
userinPassed to every hook.
maxBytesinMost bytes the body may take.
Returns
The verdict, where it stopped, and what the root held.

bindErrorName() #

const char * kleidos::json::bindErrorName(BindError error)
Parameters
NameDirectionMeaning
errorinA bind error.
Returns
Its name.

boolRow() #

template<typename T, typename F>
Member kleidos::json::boolRow(const char * key, size_t offset)
consteval

A boolean row. Use KLEIDOS_JSON_BOOL.

Template Parameters
NameMeaning
TThe struct.
FThe field's type, bool.
Parameters
NameDirectionMeaning
keyinThe JSON key.
offsetinThe field's offset in T.
Returns
The row.

boundField() #

template<typename T, typename F>
size_t kleidos::json::boundField(size_t offset)
consteval

The offset of a Bound field, checked. Use KLEIDOS_JSON_SPEC_BOUND.

Template Parameters
NameMeaning
TThe struct.
FThe field's type, which must be Bound.
Parameters
NameDirectionMeaning
offsetinThe field's offset in T.
Returns
offset.

errorName() #

const char * kleidos::json::errorName(Error error)
Parameters
NameDirectionMeaning
errorinA reader error.
Returns
Its name, for a log line or a test message.

integerRow() #

template<typename T, typename F>
Member kleidos::json::integerRow(const char * key, size_t offset, int64_t min, int64_t max)
consteval

An integer row. Use KLEIDOS_JSON_INT or KLEIDOS_JSON_UINT.

Template Parameters
NameMeaning
TThe struct.
FThe field's integer type; its signedness picks Int or UInt.
Parameters
NameDirectionMeaning
keyinThe JSON key.
offsetinThe field's offset in T.
mininLowest value accepted.
maxinHighest value accepted.
Returns
The row.

intsRow() #

template<typename T>
Member kleidos::json::intsRow(const char * key, int64_t min, int64_t max, uint32_t most, EntryFn entry)
consteval

An array-of-integers row: nothing is stored. Use KLEIDOS_JSON_INTS.

Template Parameters
NameMeaning
TThe struct the row belongs to.
Parameters
NameDirectionMeaning
keyinThe JSON key.
mininLowest value accepted.
maxinHighest value accepted.
mostinMost entries.
entryinCalled per entry.
Returns
The row.

mapSpec() #

template<typename T>
ObjectSpec kleidos::json::mapSpec(int64_t min, int64_t max, uint32_t most, EntryFn entry)
consteval

An open map: any key, each member an integer in [min, max] or a boolean, handed to entry. Use KLEIDOS_JSON_MAP.

Template Parameters
NameMeaning
TThe struct the map binds for (it holds nothing of the map).
Parameters
NameDirectionMeaning
mininLowest integer value.
maxinHighest integer value.
mostinMost members.
entryinThe hook.
Returns
The table.

objectRow() #

template<typename T, typename F>
Member kleidos::json::objectRow(const char * key, size_t offset, const ObjectSpec & child)
consteval

A nested-object row. Use KLEIDOS_JSON_OBJECT.

Template Parameters
NameMeaning
TThe struct.
FThe nested struct's type.
Parameters
NameDirectionMeaning
keyinThe JSON key.
offsetinThe field's offset in T.
childinThe table of F.
Returns
The row.

objectSpec() #

template<typename T, size_t N>
ObjectSpec kleidos::json::objectSpec(const Member(&) members[N], Unknown unknown, size_t boundAt = kNoBound)
consteval

A table: the rows of one struct, checked together.

Use KLEIDOS_JSON_SPEC or KLEIDOS_JSON_SPEC_BOUND.

Template Parameters
NameMeaning
TThe struct.
NRows.
Parameters
NameDirectionMeaning
membersinThe rows, a constexpr array that outlives the table.
unknowninWhat a key the table does not know does.
boundAtinOffset of a Bound field of T, or kNoBound.
Returns
The table.

objectsRow() #

template<typename T, typename F>
Member kleidos::json::objectsRow(const char * key, size_t offset, const ObjectSpec & child, uint32_t most, ElementFn element)
consteval

An array-of-objects row. Use KLEIDOS_JSON_OBJECTS.

Template Parameters
NameMeaning
TThe struct.
FThe element struct's type.
Parameters
NameDirectionMeaning
keyinThe JSON key.
offsetinThe element field's offset in T.
childinThe table of F.
mostinMost entries.
elementinCalled as each entry closes, before the element is wiped.
Returns
The row.

secretRow() #

template<typename T, typename F>
Member kleidos::json::secretRow(const char * key, size_t offset)
consteval

A secret row: a SecretText<N> field, never clamped. Use KLEIDOS_JSON_SECRET.

Template Parameters
NameMeaning
TThe struct.
FThe field's type, a SecretText<N>.
Parameters
NameDirectionMeaning
keyinThe JSON key.
offsetinThe field's offset in T.
Returns
The row.

sequenceLength() #

size_t kleidos::json::sequenceLength(const char * text, size_t len)

Bytes of the valid UTF-8 sequence at text, under the same rule.

Parameters
NameDirectionMeaning
textinFirst byte; may be null only when len is 0.
leninBytes 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() #

void kleidos::json::specError(const char * why)

Not constexpr on purpose: calling it from a consteval builder stops the build at the offending row. Never defined.

Parameters
NameDirectionMeaning
whyinWhat is wrong with the row or table.

textRow() #

template<typename T, typename F>
Member kleidos::json::textRow(const char * key, size_t offset, Overflow overflow)
consteval

A text row: a char[N] field. Use KLEIDOS_JSON_TEXT.

Template Parameters
NameMeaning
TThe struct.
FThe field's type, a char[N].
Parameters
NameDirectionMeaning
keyinThe JSON key.
offsetinThe field's offset in T.
overflowinWhat a longer value does.
Returns
The row.

textsRow() #

template<typename T, typename F>
Member kleidos::json::textsRow(const char * key, size_t offset, Overflow overflow, uint32_t most, EntryFn entry)
consteval

An array-of-strings row. Use KLEIDOS_JSON_TEXTS.

Template Parameters
NameMeaning
TThe struct.
FThe text field's type, a char[N] each entry is read into.
Parameters
NameDirectionMeaning
keyinThe JSON key.
offsetinThe field's offset in T.
overflowinWhat a longer entry does.
mostinMost entries.
entryinCalled per entry, before the field is wiped.
Returns
The row.

utf8Lead() #

Utf8Lead kleidos::json::utf8Lead(uint8_t byte)
constexpr

Classify a byte at or above 0x80 that starts a sequence.

Parameters
NameDirectionMeaning
byteinA 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() #

bool kleidos::json::validText(const char * text, size_t len)

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
NameDirectionMeaning
textinFirst byte; may be null only when len is 0.
leninBytes to check.
Returns
True when the span is valid.

wipe() #

void kleidos::json::wipe(void * data, size_t bytes)

Overwrite bytes bytes at data with zero, unconditionally.

Parameters
NameDirectionMeaning
dataoutStart of the block; nullptr is a no-op.
bytesinNumber of bytes to clear.

Back to the top

Variable Documentation

kMapValue #

template<typename T>
Member kleidos::json::kMapValue
inlineconstexpr

The one value rule of an open map. Use KLEIDOS_JSON_MAP.

Template Parameters
NameMeaning
TThe struct the map binds for.

kMaxMapMembers #

size_t kleidos::json::kMaxMapMembers
inlineconstexpr

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.

Back to the top