Structured Objects For Anyone
... so optimized, feels amazing.
SofaBuffers is a binary format for serializing and deserializing complex messages consisting of multiple fields, arrays, and nested structures.
See CORELIB_PLAN.md for the language-independent corelib implementation plan — the complete wire format, streaming model, API contract, testing, and benchmark requirements that every corelib-* implementation must follow.
See MESSAGE_SPEC.md for the layer above it: how the types you write in a schema (structs, unions, enums, arrays, maps) turn into those wire primitives, and when a field is written at all.
SofaBuffer data is the serialized data — the wire bytes generated by marshalling an object.
When transmitting SofaBuffer data (e.g. as an HTTP body), use the following MIME type:
application/x-sofabuffers
The x- prefix places it in the unregistered tree, so no IANA registration is required.
Recommended file extension: .sofa
What you write in a schema, and what it becomes on the wire. Several schema types share one wire encoding — the schema is what tells them apart.
| Schema type | On the wire | Note |
|---|---|---|
u8 u16 u32 u64 |
unsigned integer | one varint carries every width; the declared width bounds the value — a wider value is invalid |
i8 i16 i32 i64 |
signed integer | zig-zag |
boolean |
unsigned integer | 0 or 1 |
enum |
signed integer | the member's value; bounded by the smallest signed width holding every declared constant — a value outside it is invalid |
bitfield |
unsigned integer | the flags packed into one value; bounded by the smallest unsigned width holding the highest declared pos — a value outside it is invalid |
fp32 fp64 |
fixlen | 4 / 8 raw IEEE-754 bytes |
string |
fixlen | UTF-8, no terminator |
blob |
fixlen | opaque bytes |
struct |
sequence | its children are the fields |
union |
sequence | holds exactly one option: at most one child, whose ID picks it; if several arrive, the last one wins |
| array of numbers | array wire type | one count, then the elements |
| array of strings / blobs / structs | sequence | one child per element, ID = index |
| map | array of struct{ key, value } |
a pattern, not a type of its own |
Small examples: a u32 field with ID 0 and value 127 is 00 7f. A string field with ID 1 and value "A" is 0a 0a 41. A struct at ID 1 holding one u32 child (ID 0, value 1) is 0e 00 01 07 — start marker, the child, end marker.
Two schema attributes bound a field without ever reaching the wire: maxlen for a string/blob, and count for an array. count is a capacity — an array may carry 0 .. count elements, and the count on the wire is its actual length. An integer scalar's declared width bounds it the same way. Exceeding any of these bounds makes the message invalid.
Every field starts at its schema default, and the encoder writes only what differs. A field left at its default is simply absent, and the decoder restores it. There is no "is set" bit.
This reaches into nested structures: a struct whose every field is at its default, or a union holding its default_id option at that option's default, is left out entirely instead of being written as an empty shell — so a message in which nothing was set is zero bytes long.
A union holding any other option is the exception: that option is written even when it is at its own default — as an empty shell if it is a struct — because the option's ID is the only record of which option is held. Inside the option, the usual rule applies again. For the same reason a union option that is a string, blob or array cannot declare a non-empty default.
Arrays are where absence still carries meaning, so two rules protect them:
- An explicitly empty array is written out, because "empty" and "absent" are different values whenever the schema declares a non-empty default.
- The last element is always written, even when it equals the default — it is what tells the decoder how long the array is. So
["a", ""]and["a"]stay distinguishable. Elements before it may be skipped:["a", "", "c"]leaves a gap at index 1.
The exact rules are MESSAGE_SPEC.md §2.
Used encodings in SofaBuffers:
Integer values are transmitted as varint in SofaBuffers, regardless of the bit width of the data type. Varint (variable-length integer) encodes an integer so that small values require fewer bytes. Each byte uses the most significant bit as a continuation bit: if this bit is set, more bytes follow; otherwise, the value ends.
For signed integers, Zig-Zag encoding is also used. This maps negative numbers to positive values, allowing varint to work efficiently for small absolute values.
Specification of the SofaBuffer binary format:
The percentages for the individual field types correspond to the average usage in other message formats such as JSON, protocol buffers, and many more.
This data was used to keep the overhead for frequently used types as low as possible.
- Each field is identified by an ID. This must be unique for each sequence.
- IDs are in the range
0 .. INT32_MAX. - The type of the field is encoded in the lowest 3 bits of the ID:
| Bits | Type |
|---|---|
| 0b000 | unsigned integer |
| 0b001 | signed integer |
| 0b010 | fixlen value |
| 0b011 | array of unsigned integers |
| 0b100 | array of signed integers |
| 0b101 | array of fixlen values |
| 0b110 | sequence start |
| 0b111 | sequence end |
- The combination of ID + type is serialized using varint encoding.
- The ID is followed by at least one byte of varint-encoded unsigned integers.
- The parser requires a temporary 64-bit buffer for sequential decoding of the stream.
- If the receiver is interested in the field, the value is then written to the destination buffer as an unsigned value.
- Booleans have no dedicated wire type — they are encoded exactly as unsigned integers with the value
0(false) or1(true). The corelib provides boolean read/write helpers for this mapping; on the wire a boolean is indistinguishable from an unsigned integer. - Bitfields have no dedicated wire type — they are mapped to unsigned integers on the wire. The bit layout is handled by the generated message code; the corelib only sees a plain unsigned integer.
- The ID is followed by at least one byte of the varint-encoded unsigned integer.
- The parser requires a temporary 64-bit buffer to sequentially decode the stream.
- After decoding, the value is zig-zag decoded into a signed integer.
- If the receiver is interested in the field, the value is then written to the destination buffer as a signed value.
- Enums have no dedicated wire type — they are encoded exactly as signed integers (zig-zag varint), so on the wire an enum is indistinguishable from a signed integer. The schema still bounds it: by the smallest signed width holding every declared constant —
{RED = 1, GREEN = 2, BLUE = 3}is ani8, −128 .. 127. A value inside that width that names no constant is valid and is kept as received; only a value outside the width is invalid. Adding a constant is therefore a breaking schema change only when it widens that type. (MESSAGE_SPEC §1)
- Each fixlen field begins with fixlen length information for the value.
- Lengths are in the range
0 .. INT32_MAX. - The type of the fixlen field is encoded in the lowest three bits of the length:
| Bits | Type |
|---|---|
| 0b000 | IEEE754 32-bit float (LE) |
| 0b001 | IEEE754 64-bit double (LE) |
| 0b010 | UTF-8 string (without null termination) |
| 0b011 | BLOB (arbitrary binary data) |
| 0b100 | reserved |
| 0b101 | reserved |
| 0b110 | reserved |
| 0b111 | reserved |
- Length + type are serialized using varint encoding.
- If the receiver ignores the field, the parser can skip the bytes based on the length.
- The Fixlen length information is followed by the payload data corresponding to the specified length.
- For IEEE754 types, the endianness must be converted correctly to Little Endian depending on the system.
- IEEE754 values are stored as their raw little-endian bytes, so every value — including ±0, ±inf, and NaN — round-trips bit-for-bit.
- The ID is followed by the number of array elements.
- Array sizes are in the range
0 .. INT32_MAX, and the count is the array's actual length. A count of0is a valid, explicit empty array on the wire (header followed by a zero count, no elements). What an explicit empty array means next to an absent field is not a wire-level question — see Defaults. - This information is used by the parser to check whether all values fit into the target buffer.
- If the receiver is not interested in the field, the parser can skip the elements based on the number.
- The number of elements is followed by varint-encoded unsigned integers.
- The number of bytes can vary per element due to varint.
- The parser requires a temporary 64-bit buffer to sequentially decode the stream.
- If the receiver is interested in the field, each value is written to the corresponding offset after decoding.
- The number of elements is followed by varint-encoded unsigned integers.
- The number of bytes can vary per element due to varint.
- The parser requires a temporary 64-bit buffer to sequentially decode the stream.
- After decoding, each value is zig-zag decoded into a signed integer.
- If the receiver is interested in the field, each value is written to the corresponding offset after decoding.
- The number of elements is followed by the Fixlen length information, which applies to all elements.
- If the number of elements is
0, no Fixlen length information and no payload follow — the field is just the header and a zero count. - Arrays of dynamic types (UTF-8, BLOB) are not allowed; instead a sequence should be used for dynamic arrays.
- The following payload data is treated as consecutive fixlen values.
- A sequence exists only on the wire: it opens a new ID scope and nothing more. It has no type meaning of its own.
- A new ID scope is opened for each new sequence; conflicts with IDs of the parent sequence are not possible.
- On top of this single primitive (a fresh scope) you can model:
- Nested structures
- Arrays with a dynamic number of elements
- Arrays of variable-length elements, e.g. strings or blobs (each element may have a different length). Blobs behave just like strings — both are dynamic byte payloads.
- Tagged unions ("exactly one of") — the union opens a sequence carrying at most one child, and the id of that single field identifies the active option. A struct and a union look identical on the wire; the schema tells them apart.
- An empty sequence (a sequence start immediately followed by its end marker) is legal and must be accepted by the decoder; it is the composite-type counterpart of a zero-count array. The wire form is unconditional — when one is produced is a message-layer decision (see Defaults): an explicitly empty array, an array element that is entirely at its default, or a union option other than
default_idheld at its default. An all-defaultstructfield, or a union holdingdefault_idat its default, is omitted instead. - Sequences may nest up to a maximum depth of 255; a decoder must reject deeper nesting as a malformed message.
- This special type has no ID (value = 0).
- It signals the end of a sequence, so that the following fields belong to the parent sequence again.
- With this stop field, a sequence can be serialized without knowing its size in advance.
- When deserializing, the parser must traverse the sequence, including nested sequences if necessary, even if there is no interest in the data.
Every implementation must be streaming-capable in order to enable the incremental (chunked) transmission and reception of large messages, thereby keeping memory requirements as low as possible.
During serialization, it must be possible to process large messages without the output buffer having to be the same size as the message. Specifically, this means that during serialization, it must be possible to use a smaller output buffer than the actual message.
During deserialization, it must be ensured that the parser can be fed with arbitrarily small data chunks. This is the only way to guarantee that true streaming capability is also available on the input side.
