Details
-
Task
-
Resolution: Works as Designed
-
Major - P3
-
None
-
2.4.1
Description
I'm trying to configure the bson serialiser so that it's automatically able to serialise / deserialise types in our codebase, and also any system data types. The main issue is in determining how to automatically map constructors of all types so that any read only members will also be serialised, and passed into the correct constructor on deserialisation.
I've hit various issues and have attempted to find ways to resolve them, namely by producing a class map convention that maps constructors / properties, as well as a custom version of NamedParameterCreatorMapConvention that handles situations where the same property is defined in a sub class and its base class.
However I'm not able to get this working, so I've produced an example class structure (attached) that produces a few problems when attempting to serialise using this code:
var testDocument = new TestDocument |
{
|
Data = new SubClass(new Implementation(100), 200, 300.0) |
};
|
|
|
collection.InsertOne(testDocument);
|
The initial problems are:
- The BaseClass type contains properties that are also declared in the SubClass.
- The type of one property in the SubClass is different from the type of the same property in the base class (due to the new keyword)
I attempted to work around this by creating a MapConstructorsConvention and modifying the NamedParameterCreatorMapConvention, ensuring that properties are only mapped in the class map of the type highest up the inheritance tree, that has a constructor referencing that property. This has mostly worked but it's far from a clean solution. However there's another problem caused by this:
- The property DoubleValue on the BaseClass isn't referenced by any constructors in that class but is required by the SubClass implementation. This is where the fix i made above falls down because no types map this property.
Is there a correct / recommended approach to this? Being able to automatically call non-default constructors for all class hierarchies is quite fundamental for us.
On a related note, I see mention on here of integration with JSON.Net which is able to handle these types. What is the status of this?
Thanks