data.encoder #
crystal binary encoder protocol
Made for performance and smallest possible binary size
Its a very simple implementation, it just follows the order of the object and concatenate binary representations of the original information. This should lead in very fast serialization and smallest possible binary format
downsides
- if mistakes are made in encoding, data will be lost, its very minimal
example
import encoder
mut e := encoder.new()
a := AStruct{
items: ['a', 'b']
nr: 10
privkey: []u8{len: 5, init: u8(0xf8)}
}
e.add_list_string(a.items)
e.add_int(a.nr)
_, privkey := ed25519.generate_key()!
e.add_bytes(privkey)
println(e.data)
mut d := encoder.decoder_new(e.data)
mut aa := AStruct{}
aa.items = d.get_list_string()
aa.nr = d.get_int()
aa.privkey = d.get_bytes()
assert a == aa
fn decode #
fn decode[T](data []u8) !T
fn decoder_new #
fn decoder_new(data []u8) Decoder
fn encode #
fn encode[T](obj T) ![]u8
example see https://github.com/vlang/v/blob/master/examples/compiletime/reflection.v
fn new #
fn new() Encoder
enum DataType{ string int bytes u8 u16 u32 u64 time list_string list_int list_u8 list_u16 list_u32 list_u64 map_string map_bytes }
struct Decoder #
struct Decoder {
pub mut:
version u8 = 1 // is important
data []u8
}
fn (Decoder) get_string #
fn (mut d Decoder) get_string() string
fn (Decoder) get_int #
fn (mut d Decoder) get_int() int
fn (Decoder) get_bytes #
fn (mut d Decoder) get_bytes() []u8
fn (Decoder) get_u8 #
fn (mut d Decoder) get_u8() u8
adds u16 length of string in bytes + the bytes
fn (Decoder) get_u16 #
fn (mut d Decoder) get_u16() u16
fn (Decoder) get_u32 #
fn (mut d Decoder) get_u32() u32
fn (Decoder) get_u64 #
fn (mut d Decoder) get_u64() u64
fn (Decoder) get_i64 #
fn (mut d Decoder) get_i64() i64
fn (Decoder) get_time #
fn (mut d Decoder) get_time() time.Time
fn (Decoder) get_ourtime #
fn (mut d Decoder) get_ourtime() ourtime.OurTime
fn (Decoder) get_list_string #
fn (mut d Decoder) get_list_string() []string
fn (Decoder) get_list_int #
fn (mut d Decoder) get_list_int() []int
fn (Decoder) get_list_u8 #
fn (mut d Decoder) get_list_u8() []u8
fn (Decoder) get_list_u16 #
fn (mut d Decoder) get_list_u16() []u16
fn (Decoder) get_list_u32 #
fn (mut d Decoder) get_list_u32() []u32
fn (Decoder) get_list_u64 #
fn (mut d Decoder) get_list_u64() []u64
fn (Decoder) get_map_string #
fn (mut d Decoder) get_map_string() map[string]string
fn (Decoder) get_map_bytes #
fn (mut d Decoder) get_map_bytes() map[string][]u8
struct Encoder #
struct Encoder {
pub mut:
data []u8
// datatypes []DataType
}
fn (Encoder) add_string #
fn (mut b Encoder) add_string(data string)
adds u16 length of string in bytes + the bytes
fn (Encoder) add_int #
fn (mut b Encoder) add_int(data int)
Please note that unlike C and Go, int is always a 32 bit integer. We borrow the add_u32() function to handle the encoding of a 32 bit type
fn (Encoder) add_bytes #
fn (mut b Encoder) add_bytes(data []u8)
add bytes or bytestring
fn (Encoder) add_u8 #
fn (mut b Encoder) add_u8(data u8)
fn (Encoder) add_u16 #
fn (mut b Encoder) add_u16(data u16)
fn (Encoder) add_u32 #
fn (mut b Encoder) add_u32(data u32)
fn (Encoder) add_u64 #
fn (mut b Encoder) add_u64(data u64)
fn (Encoder) add_i64 #
fn (mut b Encoder) add_i64(data i64)
fn (Encoder) add_time #
fn (mut b Encoder) add_time(data time.Time)
fn (Encoder) add_ourtime #
fn (mut b Encoder) add_ourtime(data ourtime.OurTime)
fn (Encoder) add_list_string #
fn (mut b Encoder) add_list_string(data []string)
fn (Encoder) add_list_int #
fn (mut b Encoder) add_list_int(data []int)
fn (Encoder) add_list_u8 #
fn (mut b Encoder) add_list_u8(data []u8)
fn (Encoder) add_list_u16 #
fn (mut b Encoder) add_list_u16(data []u16)
fn (Encoder) add_list_u32 #
fn (mut b Encoder) add_list_u32(data []u32)
fn (Encoder) add_list_u64 #
fn (mut b Encoder) add_list_u64(data []u64)
fn (Encoder) add_map_string #
fn (mut b Encoder) add_map_string(data map[string]string)
when complicated hash e.g. map of other object need to serialize each sub object
fn (Encoder) add_map_bytes #
fn (mut b Encoder) add_map_bytes(data map[string][]u8)
when complicated hash e.g. map of other object need to serialize each sub object