digitalmars.D.learn - Object Serialization?
- dcoder (5/5) Apr 24 2012 Hello.
- David Nadlinger (6/9) Apr 24 2012 There is no standard library support yet, but you might want to
- Jordi Sayol (5/10) Apr 26 2012 and both, orange and msgpack available as deb packages on d-apt server:
- Jacob Carlborg (9/14) Apr 24 2012 You can have a look at Orange:
- sclytrack (22/62) Apr 24 2012 Does it automatically pick everything to serialize?
- Jacob Carlborg (43/90) Apr 25 2012 It depends on what you want to do.
- Dejan Lekic (8/15) Apr 27 2012 You have following
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
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
Al 24/04/12 18:45, En/na David Nadlinger ha escrit:On Tuesday, 24 April 2012 at 16:42:19 UTC, dcoder wrote:and both, orange and msgpack available as deb packages on d-apt server: https://code.google.com/p/d-apt/wiki/APT_Repository -- Jordi SayolI'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).
Apr 26 2012
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
On 04/24/2012 08:50 PM, Jacob Carlborg wrote:On 2012-04-24 18:42, dcoder wrote: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.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.htmlBasic 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
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.Yeah, I know.Basic Example Serialize Through Base Class Register Serializer?There is no register serializer example.Yes.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.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
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