Briar: Serialisation

This document describes the serialisation format used by the Briar messaging protocol. The format is broadly similar to other space-efficient binary formats such as MessagePack.

The format's primitive types are boolean, uint7, int8, int16, int32, int64, float32, float64, string, raw and null. Lists, maps and structs can be constructed from these primitives, and can be nested.

All integers use big-endian two's complement representation, all floating point numbers use IEEE 754, and all strings use UTF-8.

Tags

The type of each serialised object can be identified by examining its first byte, which is called a tag: Some of the primitive types (boolean, uint7, null) encode the value in the same byte as the tag, so the whole object occupies a single byte.

Examples

Structs

A struct is an ordered sequence of other types, which are called its components. A struct's type definition lists the order and types of its components, and whether each component is optional or mandatory. If an optional component is absent from a struct then its place must be marked by a null. The type definition may place constraints on the values of components.

Structs can contain primitive types, lists, maps, and other structs. Up to 256 structs can be defined, each of which is assigned an identifier between 0 and 255 inclusive.

As an example, we define two structs: cat and lolcat. A cat consists of a mandatory name, represented as a string of 1 - 30 UTF-8 bytes; an optional age in days, represented as a positive integer; and an optional sex, represented as a boolean, where true represents male and false represents female. A lolcat consists of a mandatory cat and a mandatory caption; the caption is represented as a string of 1 - 100 UTF-8 bytes. The struct cat is assigned the identifier 0, while lolcat is assigned the identifier 1.

The structs used by the Briar messaging protocol are described in a separate document.

Compact Encodings

Short strings, raws, lists and maps can be represented in a compact way by encoding the length in the tag, so the object is only one byte larger than its contents.

Structs with identifiers less than 32 can be encoded in a similar way, so identifiers less than 32 should be used for structs that are expected to be used frequently.

Examples

Home | Technical Overview | Protocol Spec | Mailing List | Source Code