msgpack_ll

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:

  • When calling parseType the compile time type must match the actual data type or incorrect results will be returned. Use getType to verify the type before calling parseType.
  • The fix types have certain maximum and minimum values. These conditions need to be ensured when calling formatType!T:
    • MsgpackType.posFixInt: Value must satisfy value < 128
    • MsgpackType.negFixInt: Value must satisfy -33 < value < 0
    • MsgpackType.fixStr: Length must satisfy length < 32
    • MsgpackType.fixArray: Length must satisfy length < 16
    • MsgpackType.fixMap: Length must satisfy length < 16
    • All ext types: extType must satisfy extType < 128

    Other size restrictions are automatically enforced by proper typing.

  • The debug=DebugMsgpackLL debug version can be used to enable debug checks for these problems.
  • ubyte[] data = ...;
    byte[] result;
    
    // First read array length
    enforce(getType(data[0]) == MsgpackType.array16);
    auto length = parseType!(MsgpackType.array16)(data[0..DataSize!(MsgpackType.array16)]);
    data = data[DataSize!(MsgpackType.array16) .. $];
    
    // Then read array values
    for(size_t i = 0; i < length; i++)
    {
        enforce(getType(data[0]) == MsgpackType.int8);
        result ~= parseType!(MsgpackType.int8)(data[0..DataSize!(MsgpackType.int8)]);
        data = data[DataSize!(MsgpackType.int8) .. $];
    }
    Proper formatting and parsing of complex types (maps, arrays, ext types) needs help from the API user and must be done according to the MessagePack specification. For example to parse an array16 of int8:
  • 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.

    Members

    Enums

    DataSize
    eponymoustemplate DataSize(MsgpackType type)

    Get serialized data size at compile time.

    MsgpackType
    enum MsgpackType

    Enum of MessagePack types.

    Functions

    formatType
    void formatType(typeof(null) value, ubyte[DataSize!type] data)
    void formatType(ubyte[DataSize!type] data)
    void formatType(bool value, ubyte[DataSize!type] data)
    void formatType(ubyte value, ubyte[DataSize!type] data)
    void formatType(byte value, ubyte[DataSize!type] data)
    void formatType(ushort value, ubyte[DataSize!type] data)
    void formatType(uint value, ubyte[DataSize!type] data)
    void formatType(ulong value, ubyte[DataSize!type] data)
    void formatType(short value, ubyte[DataSize!type] data)
    void formatType(int value, ubyte[DataSize!type] data)
    void formatType(long value, ubyte[DataSize!type] data)
    void formatType(float value, ubyte[DataSize!type] data)
    void formatType(double value, ubyte[DataSize!type] data)
    void formatType(ubyte length, ubyte[DataSize!type] data)
    void formatType(ushort length, ubyte[DataSize!type] data)
    void formatType(uint length, ubyte[DataSize!type] data)
    void formatType(ubyte extType, ubyte[1] value, ubyte[DataSize!type] data)
    void formatType(ubyte extType, ubyte[2] value, ubyte[DataSize!type] data)
    void formatType(ubyte extType, ubyte[4] value, ubyte[DataSize!type] data)
    void formatType(ubyte extType, ubyte[8] value, ubyte[DataSize!type] data)
    void formatType(ubyte length, ubyte extType, ubyte[DataSize!type] data)
    void formatType(ushort length, ubyte extType, ubyte[DataSize!type] data)
    void formatType(ubyte extType, ubyte[16] value, ubyte[DataSize!type] data)
    void formatType(uint length, ubyte extType, ubyte[DataSize!type] data)

    Serialize a value to a certain type.

    getDataSize
    size_t getDataSize(MsgpackType type)

    Get serialized data size at runtime. DataSize!() should be preferred if the type is known at compile time.

    getType
    MsgpackType getType(ubyte value)

    Look at the first byte of an object to determine the type.

    parseType
    auto parseType(ubyte[DataSize!type] data)
    ubyte[DataSize!type - 1] parseType(ubyte[DataSize!type] data)

    Parses the MessagePack object with specified type.

    testExt
    void testExt(T length, ubyte extType)
    Undocumented in source. Be warned that the author may not have intended to support it.
    testFixExt
    void testFixExt(ubyte extType, T value)
    Undocumented in source. Be warned that the author may not have intended to support it.
    testFormat
    void testFormat(T value)
    Undocumented in source. Be warned that the author may not have intended to support it.
    testFormatNoArg
    void testFormatNoArg(T value)
    Undocumented in source. Be warned that the author may not have intended to support it.

    Structs

    ExtType
    struct ExtType

    Serialization information about an ext type.

    Meta