Cerealed v0.2.0 - a(nother) D serialisation library

Jacob Carlborg doob at me.com
Tue Nov 19 08:03:07 PST 2013


On 2013-11-19 14:24, Atila Neves wrote:

> It serialises the slice and restores it as it was, not the data behind
> it. I should add some tests for that.

class Foo
{
     int[] a;
     int[] b;
}

auto foo = new Foo;
foo.a = [1, 2, 3, 4];
foo.b = foo.a[1 .. 3];

auto cereal = new Cerealiser();
cereal ~= foo;

auto decerealiser = new Decerealiser(cereal.bytes);
auto foo2 = decerealiser.value!(Foo);
foo2.b[0] = 10;
assert(foo2.a[1] == 10); // will this pass?

> I'm not sure I understand the question.

auto cereal = new Cerealiser();
cereal ~= foo;
cereal ~= foo; // will this be serialized a second time or will a 
reference to it be serialized?

> No. I can't see how that would be possible without implementing an
> interface (I may well be wrong), and that was the kind of thing I really
> didn't want to do. Not only that, but for the kind of code I write, I
> either want to send something to the network, in which case I know which
> class to instantiate and don't need a base class reference, or I want to
> unmarshall bytes that came in. In the latter case there's usually an
> identifier in the header to tell the factory method which class to
> instantiante. So that doesn't usually come up.

Even if you know the identifier in the header file you need a static 
type when deserializing:

decerealiser.value!(Foo)

Even if you can instantiate the right subclass you need to have the 
static type information to deserialize it.

It is possible to do, as I have done in Orange. The requirement is to 
register the subclasses that need to be serialized through a base class 
reference. See 
https://github.com/jacob-carlborg/orange/blob/master/orange/serialization/Serializer.d#L241..L262 
and 
https://github.com/jacob-carlborg/orange/blob/master/orange/serialization/Serializer.d#L787..L788

-- 
/Jacob Carlborg


More information about the Digitalmars-d-announce mailing list