Am 21.03.2014 23:26, schrieb Colin Grogan:

On Fri, 21 Mar 2014 08:23:27 GMT, Sönke Ludwig wrote:

On Thu, 20 Mar 2014 21:59:55 GMT, Colin Grogan wrote:

Hi Sonke,

Is there currently any plans to generically convert any Struct or Class type into Bson for insertion into MongoDB?

Something that would work like the following:

struct MyStruct{ int a; }
MyStruct myStruct = MyStruct(1);
Bson bson = Bson(myStruct);

MyStruct theCopy = bson.get!MyStruct;
assert(myStruct == theCopy);

Ideally, I think thats a pretty nice usage scenario.

At first glance I dont think it would be a huge job to add to the framework.

A couple of templates to test whether a struct/class has the functions to convert to and from Bson, and an addition to the Type enum in the Bson module to accept custom objects, which would then go and call the relevant functions.
Of course, it would still be up to the programmer to provide the necessary functions to do the actual conversion.
I think it would make talking to the DB pretty nice and clean to use.

What do you think? Would it be useful?
If something like that isnt already there of course, you seem to have thought of everything else! :)

That's serializeToBson(myStruct) and deserialize!MyStruct(bson)! The methods in MongoCollection also all call serializeToBson implicitly, so it's also possible to just call for example coll.insert(myStruct).

By default, a type will be serialized field by field (or property by property), but it's also possible to define toBson/fromBson, toJson/fromJson or toString/fromString to override the default behavior. There are also a few attributes that can be used to modify the behavior, see the functions section of vibe.data.serialization.

Ah ok. I must've missed it when reading the documentation. Thanks!

There are indeed still holes in the documentation, I'll add some
relevant sentences for this.