Object Serialization?

Jacob Carlborg doob at me.com
Wed Apr 25 00:10:45 PDT 2012


On 2012-04-24 23:30, sclytrack wrote:

> Does it automatically pick everything to serialize?

Yes.

> How would you make it more selective?

It depends on what you want to do.

> struct Me
> {
> int x;
> int y;
> }
>
> serialize x but not y.

In this simple case you can do like this:

struct Me
{
     int x;
     int y;

     mixin NonSerialized!(y);
}

> Would you have to create a custom serializer?

No, not in this case. But it depends on what you want to do.

> If so I would like to see an example with the custom serializer and
> deserializer that only does the x.
>
>
>> Basic Example
>> Serialize Through Base Class
>> Register Serializer?
>
>
> There is no register serializer example.

Yeah, I know.

>> 277 private void serializeStruct (T) (T value, string key, Id id)
>> 278 {
>> 279 string type = T.stringof;
>> 280
>> 281 triggerEvents(serializing, value, {
>> 282 archive.archiveStruct(type, key, id, {
>> 283 if (type in serializers)
>> 284 {
>> 285 auto wrapper = getSerializerWrapper!(T)(type);
>> 286 wrapper(value, this, key);
>> 287 }
>> 288
>> 289 else
>> 290 {
>> 291 static if (isSerializable!(T))
>> 292 value.toData(this, key);
>> 293
>> 294 else
>> 295 objectStructSerializeHelper(value);
>> 296 }
>> 297 });
>> 298 });
>> 299 }
>
>
> I assume that the wrapper is the custom serializer that can be registered.

Yes.

> Then there is the toData that a struct can have, basically
> member functions that do the serialization. Priority is
> given to the registered serializer over the member functions.

Yes.

> And the last one. ObjectStructSerializerHelper is more a default
> serializer if none is registered or doesn't have the correct
> isSerializable member functions.
> ObjectStructSerializerHelper(T) (T .....)
>
>
> Just browsing, I haven't downloaded anything.

You can have a look at "registerSerializer" and "registerDeserializer" here:

http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializer.html

Most of the documented classes and methods contains examples.

There are basically three ways to customize the (de)serialization process:

* Using NonSerialized - 
http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializable.html

* Non-intrusive serialization (registerSerializer) - 
http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializer.html

* Intrusive serialization (toData) - 
http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializable.html

So in your example it would look something like this:

struct Me
{
     int x;
     int y;
}

auto archive = new XmlArchive!();
auto serializer = new Serializer(archive);

auto dg = (Me value, Serializer serializer, Data key) {
     serializer.serialize(x, "x");
};

auto dg2 = (ref Me value, Serializer serializer, Data key) {
     value.x = serializer.deserialize!(int)("x");
};

Serializer.registerSerializer!(Me)(dg);
Serializer.registerDeserializer!(Me)(dg2);

The above would be the non-intrusive example.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list