www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Object Serialization?

reply "dcoder" <dcoder nowhere.com> writes:
Hello.

I'm probably not looking hard enough, but.... Do we have any 
standard d-library for serializing an object/object tree into 
-for example- an xml file?

thanks.
Apr 24 2012
next sibling parent "David Nadlinger" <see klickverbot.at> writes:
On Tuesday, 24 April 2012 at 16:42:19 UTC, dcoder wrote:
 I'm probably not looking hard enough, but.... Do we have any 
 standard d-library for serializing an object/object tree into 
 -for example- an xml file?

There is no standard library support yet, but you might want to look at Orange (full fledged D serialization library) or the msgpack/Thrift D implementations (fast, lightweight, written with primarily RPC in mind). David
Apr 24 2012
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2012-04-24 18:42, dcoder wrote:
 Hello.

 I'm probably not looking hard enough, but.... Do we have any standard
 d-library for serializing an object/object tree into -for example- an
 xml file?

 thanks.

You can have a look at Orange: https://github.com/jacob-carlborg/orange Tutorials: http://dsource.org/projects/orange/wiki/Tutorials API reference: http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializer.html -- /Jacob Carlborg
Apr 24 2012
parent reply sclytrack <sclytrack ars.com> writes:
On 04/24/2012 08:50 PM, Jacob Carlborg wrote:
 On 2012-04-24 18:42, dcoder wrote:
 Hello.

 I'm probably not looking hard enough, but.... Do we have any standard
 d-library for serializing an object/object tree into -for example- an
 xml file?

 thanks.

You can have a look at Orange: https://github.com/jacob-carlborg/orange Tutorials: http://dsource.org/projects/orange/wiki/Tutorials API reference: http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializer.html

Does it automatically pick everything to serialize? How would you make it more selective? struct Me { int x; int y; } serialize x but not y. Would you have to create a custom serializer? 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.
 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. 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. 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.
Apr 24 2012
parent Jacob Carlborg <doob me.com> writes:
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
Apr 25 2012
prev sibling next sibling parent Jordi Sayol <g.sayol yahoo.es> writes:
Al 24/04/12 18:45, En/na David Nadlinger ha escrit:
 On Tuesday, 24 April 2012 at 16:42:19 UTC, dcoder wrote:
 I'm probably not looking hard enough, but.... Do we have any standard
d-library for serializing an object/object tree into -for example- an xml file?

There is no standard library support yet, but you might want to look at Orange (full fledged D serialization library) or the msgpack/Thrift D implementations (fast, lightweight, written with primarily RPC in mind).

and both, orange and msgpack available as deb packages on d-apt server: https://code.google.com/p/d-apt/wiki/APT_Repository -- Jordi Sayol
Apr 26 2012
prev sibling parent Dejan Lekic <dejan.lekic gmail.com> writes:
dcoder wrote:

 Hello.
 
 I'm probably not looking hard enough, but.... Do we have any
 standard d-library for serializing an object/object tree into
 -for example- an xml file?
 
 thanks.

You have following - Orange - Thrift implementation - BSON (http://vibed.org) Probably wrappers or bindings to (all) other serialisation libraries as well. Regards
Apr 27 2012