2
votes

In MongoDB I've a class with a property MyProperty of type object.

public MyClass
{
    public object MyProperty;
    public string Prop1;
    public DateTime Prop2;
    public int Prop3;
}

Serialization to MongoDB works without any problem creating a JSon of this type:

MyProperty" : {
                "_t" : "ExampleClass",
            [...]
}

But when I try do seserialize it I get the following error:

An error occurred while deserializing the MyProperty property of class MyClass: Unknown discriminator value 'ExampleClass'.

I'd like to deserialize MyProperty to a simple generic BsonDocument or to a string.

1
Please elaborate your question with supporting sample documents. Also what do you mean by property only? You can use bson deserializer to convert any valid json to bson document.Saleem
@Saleem I've tried to clarify the example. I want only the property MyProperty to be read as a BsonDocument. The other properties of myclass must be deserialized to their type.Revious
how about changing your property type to BsonDocument instead of object?Saleem
@Saleem if possible I'd like to avoid.. just because we have already data formatted in that way and also for accessing them more easily through RoboMongo...Revious

1 Answers

5
votes

The _t stores your custom class name. You will need to register this custom class mapping in order for mongo to know which object to use when deserializing. Here is a code example (should only need to call this once at the beginning of your application):

if (!BsonClassMap.IsClassMapRegistered(typeof(ExampleClass)))
{
   BsonClassMap.RegisterClassMap<ExampleClass>();
}