Type Keys and Data Serialization
Serialization across fundamental types is a major WIP. Ideally a serialization system is fast, lightweight, and packs bytes tightly (to be suitable in small embedded networks). It also needs to be self describing, althought perhaps not in every case (i.e. serialize_safe() and serialize_tight() variants).
Fundamental types are described with pre-determined keys.
void | int | bool | float | string |
0 | 1 | 2 | 3 | 4 |
Serializations of these are straightforwards, i.e.:
template<typename T>
void serialize(T var, uint8_t* buffer, size_t* wptr){}
template<>inline
void serialize<int>(int var, uint8_t* buffer, size_t* wptr){
buffer[(*wptr) ++] = var & 255;
buffer[(*wptr) ++] = (var >> 8) & 255;
buffer[(*wptr) ++] = (var >> 16) & 255;
buffer[(*wptr) ++] = (var >> 24) & 255;
}
template<>inline
void serialize<bool>(bool var, uint8_t* buffer, size_t* wptr){
buffer[(*wptr) ++] = var ? 1 : 0;
}
template<typename T>
T deserialize(uint8_t* buffer, size_t* rptr){}
template<>inline
int deserialize<int>(uint8_t* buffer, size_t* rptr){
int val = 0;
val |= buffer[(*rptr ++)];
val |= buffer[(*rptr ++)] << 8;
val |= buffer[(*rptr ++)] << 16;
val |= buffer[(*rptr ++)] << 24;
return val;
}
template<>inline
bool deserialize<bool>(uint8_t* buffer, size_t* rptr){
return buffer[(*rptr ++)];
}
At the moment, serdes is all “tight” - except for strings, which we pack with their type key and a length field. We do not pack a zero-delimiter, as the length field makes this reduntant.
This is subject to change: the length byte has max 255 chars (but otherwise makes deserialization easier)… longer packs are likely to transport in multiple packets, but i.e. IO between two processes on the CPU are likely to use extremely wide data channels, so allowing for longer lengths is desireable.
STR_KEY | char count | char 1 | ... | char n |
4 | 0-255 | ... | ... | ... |