Skip to content

Data Serialization Format

Nur edited this page Jul 19, 2023 · 3 revisions

Primitive Types

Number

  • u8, i8, f32, f64 are encoded in little-endian bytes order.
  • u16, u32, u64, u128, usize are encoded in LEB128
  • i16, i32, i64, i128, isize are first encoded with ZigZag code, which transforms signed integers into unsigned integers with a smaller range, and then they are further encoded using LEB128

Boolean

Boolean value encoded in a single byte.

  • false is represented by the value 0
  • true is represented by the value 1

Result And Optional Type

The Result<T, E> type is a Tagged Union type that can represent either a value of type T or an error value of type E. Also Option<T> can represent either a value of type T or the absence of a value.

The first byte of the encoded value is used as a discriminant to indicate which variant of the type is being represented.

For the Result<T, E> type, a discriminant value of 1 indicates that the computation succeeded and 0 indicates that the computation failed.

For the Option<T> type, a discriminant value of 0 indicates that the computation returned no value, and 1 indicates that the computation returned some value.

Tuples And Array

For both tuples and array, The length is not encoded. And each value is encoded sequentially.

Collection

Examples of collections are String, BTreeSet, HashSet, BinaryHeap, LinkedList, VecDeque, Vec, HashMap, BTreeMap.

First the length of the collection is encoded using BEU30, It is a variable-length encoder type for non-negative integer values.

Then each entry is encoded sequentially. For String type, the actual data in encoded using UTF-8 format.