I\'m working with C# MongoDB driver and I have this rather complex JSON struct to save:
{
\"name\" : \"value\",
\"age\": 1,
\"isFemale\": true,
A simple way for dealing with this issue and not have to create additional properties, is registering a custom serializer.
ComplexTypeSerializer.cs
namespace MyNamespace.MongoDB.Serializers
{
public class ComplexTypeSerializer : SerializerBase
And register it as per http://mongodb.github.io/mongo-csharp-driver/2.3/reference/bson/serialization/
BsonSerializer.RegisterSerializer(typeof(IDictionary), new ComplexTypeSerializer());
or
BsonSerializer.RegisterSerializer(typeof(IList>), new ComplexTypeSerializer());
The code below was tested with a simple IDictionary
, not sure if it would work with IList
, however if it's not supported, you can create another custom serializer to support it.