EdB EdB - 1 year ago 330
JSON Question

C# MongoDB deserialization of id property

I have a class that looks like this:

public class Object
public ObjectId Id { get; set; }
public List<Record> Records { get; set; }

public class Record
public ObjectId Id { get; set; }
public Dictionary<string, object> Values { get; set; }

I'm trying to deserialize the following json:

"Id": "5791fad6fcef4434d0318d69",
"Values": {
"Name": "some name....",
"Account": {
"Value": "123",
"Name": "Test Account"

With the mongodb c# driver:

var record = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<Record>(BsonDocument.Parse(value.ToString()));

But I keep receiving the error: Element 'Id' does not match any field or property of class Record.

Any ideas as to why it won't recognize the id property?

Answer Source

It turns out that if you change the property name to the following then the object is deserialized correctly:

public ObjectId _id { get; set; }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download