Allocation-Free Serialization

This library supports efficient serialization/deserialization of most relevant data types into byte slices. Message type and data type encoding are supported, including variable length encodings. No support for stacked structs exists or will be included.

It uses only pre-allocated memory so that manual allocation and thus memory protection schemes are accessible.

Two methods for variable allocation are availble: With reflection and without reflection - based only on language type annotations. This simplifies development (reflect) while making it very easy to switch to efficient runtime.

 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
type data struct {
	a int16
	b int32
	c int64
	d []byte
}

// ...

v := &data{
	a: 1,
	b: 29381,
	c: 5098123,
	d: []byte("any string"),
   }

encodingScheme := []interface{}{v.a, v.b, v.c, v.d} // Define fields to include and their order in the output message.
size, _ := EncodeSize(encodingScheme...)            // Calculate size of encoded message for manual allocation.
encodedData, _ := Encode(buf, encodingScheme...)    // Serialize data into message using preallocated buffer and the defined scheme.
_, _ := Decode(encodedData, encodingScheme)         // Decode message into the pre-allocated variable decribed by encodingScheme.

Get the source here: binencode