I am having an issue with serializing BsonDocuments using BsonSerializer.
I am using var bsonDoc = collection.Find(...)
to pull a single document from a MongoDB database.
I then try to serialize it using
var strongTypedDocument = BsonSerializer.Deserialize<MainSDocument>(bsonDoc);
Here is the BsonDocument
{
"MainSPayload" :
{
"GDeets" : { "Id" : 0, "GSerial" : "XX123XX123" }
}
}
Here are the C# classes
[JsonObject(MemberSerialization.OptIn)]
[BsonIgnoreExtraElements]
public class MainSDocument
{
[JsonProperty(Required = Required.Always)]
public MainState MainSPayload
{
get; set;
}
}
[JsonObject(MemberSerialization.OptIn)]
public class MainState
{
[JsonProperty(Required = Required.Always)]
public GDetails GDeets
{
get; set;
}
}
public class GDetails
{
public int Id
{
get; set;
}
public string GSerial
{
get; set;
}
}
The error I am getting is: FormatException: An error occurred while deserializing the GDetails property of class MainState : Element 'Id' does not match any field or property of class GDetails .'
Why is this happening? Why doesn't the element Id
match the int element Id
in the GDetails
class?