http://code.dlang.org/packages/cerealed
https://github.com/atilaneves/cerealed

But what about Orange / std.serialization? Well,
1. I started this before I knew about Orange. This is actually based on a library I wrote in C++ (https://bitbucket.org/atilaneves/cereal) for the game I'm trying to finish, and it dawned on me it would probably be easier in D. It was. 2. Orange serialises to XML, I need/want binary. I could've written a binary archive, I guess, but I'd rather do this.
3. I would've written this for fun anyway.

The deal here is binary serialisation with minimal to no boilerplate. As mentioned in the README, I used this to implement a MQTT broker with minimal fuss. Serialising classes, structs, and normal D types requires writing nothing at all. Custom serialisation can be handled in two different ways and both of them require writing only one function for both struct/class -> bytes and vice-versa. Since a code sample is worth a 1000 words:

    import cerealed; //uses package.d from 2.064

    struct MyStruct {
        ubyte mybyte1;
        @NoCereal uint nocereal1; //won't be serialised
        //the next 3 members will all take up one byte
        @Bits!4 ubyte nibble; //gets packed into 4 bits
        @Bits!1 ubyte bit; //gets packed into 1 bit
        @Bits!3 ubyte bits3; //gets packed into 3 bits
        ubyte mybyte2;
    }

    auto enc = new Cerealiser();
    enc ~= MyStruct(3, 123, 14, 1, 2, 42);
    assert(enc.bytes == [ 3, 0xea /*1110 1 010*/, 42]);

auto dec = new Decerealizer([ 3, 0xea, 42]); //US spelling works too
    //the 2nd value is 0 and not 123 since that value
    //doesn't get serialised/deserialised
    assert(dec.value!MyStruct == MyStruct(3, 0, 14, 1, 2, 42));

Custom serialisation is explained in the README. I had to use it for my MQTT project and it worked quite well.

I still need to add a bunch of negative tests and accompanying code but this is good enough to fool around with for the moment.

Atila

Reply via email to