Enum of MessagePack types.
Serialize a value to a certain type.
Get serialized data size at runtime. DataSize!() should be preferred if the type is known at compile time.
Look at the first byte of an object to determine the type.
Parses the MessagePack object with specified type.
Serialization information about an ext type.
Most types are handled like this:
1 ubyte[128] buffer; 2 enum type = MsgpackType.uint8; 3 4 // Serialization 5 formatType!(type)(42, buffer[0 .. DataSize!type]); 6 7 // Now deserialize 8 // Get the type at runtime 9 assert(getType(buffer[0]) == type); 10 // and deserialize specifying the type at compile time 11 const result = parseType!type(buffer[0 .. DataSize!type]); 12 assert(result == 42);
Values for nil, true8 and false8 are ignored and can be skipped:
1 ubyte[128] buffer; 2 enum type = MsgpackType.true8; 3 4 // Serialization 5 formatType!(type)(buffer[0 .. DataSize!type]); 6 7 // Now deserialize 8 // Get the type at runtime 9 assert(getType(buffer[0]) == type); 10 // and deserialize specifying the type at compile time 11 const result = parseType!type(buffer[0 .. DataSize!type]); 12 assert(result == true);
The fixExt types accept an additional extType parameter and data:
1 ubyte[128] buffer; 2 ubyte[1] value = [1]; 3 enum type = MsgpackType.fixExt1; 4 5 // Serialization 6 formatType!(type)(42, value, buffer[0 .. DataSize!type]); 7 8 // Now deserialize 9 // Get the type at runtime 10 assert(getType(buffer[0]) == type); 11 const result = parseType!type(buffer[0 .. DataSize!type]); 12 // and deserialize specifying the type at compile time 13 assert(result[0] == 42); 14 assert(result[1 .. $] == value);
The ext types accept an additional extType parameter and data length.
1 ubyte[128] buffer; 2 enum type = MsgpackType.ext8; 3 4 // Serialization 5 formatType!(type)(10, 42, buffer[0 .. DataSize!type]); 6 7 // Now deserialize 8 // Get the type at runtime 9 assert(getType(buffer[0]) == type); 10 // and deserialize specifying the type at compile time 11 const result = parseType!type(buffer[0 .. DataSize!type]); 12 assert(result.type == 42); 13 assert(result.length == 10);
Often you'll want to decode multiple possible types:
1 ulong decodeSomeUint(ubyte[] data) { 2 switch (data[0].getType()) { 3 case MsgpackType.posFixInt: 4 return parseType!(MsgpackType.posFixInt)(data[0 .. DataSize!(MsgpackType.posFixInt)]); 5 case MsgpackType.uint8: 6 return parseType!(MsgpackType.uint8)(data[0 .. DataSize!(MsgpackType.uint8)]); 7 case MsgpackType.uint16: 8 return parseType!(MsgpackType.uint16)(data[0 .. DataSize!(MsgpackType.uint16)]); 9 case MsgpackType.uint32: 10 return parseType!(MsgpackType.uint32)(data[0 .. DataSize!(MsgpackType.uint32)]); 11 case MsgpackType.uint64: 12 return parseType!(MsgpackType.uint64)(data[0 .. DataSize!(MsgpackType.uint64)]); 13 default: 14 throw new Exception("Expected integer type"); 15 } 16 }
A low-level pure @nogc and betterC MessagePack implementation.
Note: As this is a low-level implementation certain error checking a some handling of the MessagePack data format has to be done by the API user. The following conditions need to be ensured by the user:
Other size restrictions are automatically enforced by proper typing.
Requires only std.bitmanip for bigEndianToNative and nativeToBigEndian as external dependency.
TODO: Could try to avoid that dependency. This is only a compile time dependency anyway though, as these functions are templates and get inlined into this module.