{
"_id" : ObjectId("4fbb728d80db260988580e05"),
"titleFull" : "Foo, Inc",
"titleShort" : "Foo",
"countries" : [
ObjectId("4fba04ef80db260988f8b607"),
ObjectId("4fba05f880db260988cd5cfd") ],
"type" : "company"
}
And such class in ASP.NET MVC 4 Web API project:public class Company
{
[BsonRepresentation(BsonType.ObjectId)]
public String id { get; set; }
public String titleFull { get; set; }
public String titleShort { get; set; }
//[BsonRepresentation(BsonType.ObjectId)]
//public String[] countries { get; set; } — not working
public ObjectId[] countries { get; set; }
public String type { get; set; }
}
When I'm sending GET request on /api/countries I receive JSON document (It's mvc deserialization):{
"id": "4fba097e80db2609886ce7f2",
"titleFull": "Foo, LLC",
"titleShort": "Foo",
"countries": [
{
"_increment": 16299527
"_machine": 8444710
"_pid": 2440
"_timestamp": 1337591023
},
{
"_increment": 13458685
"_machine": 8444710
"_pid": 2440
"_timestamp": 1337591288
}
],
"type": "company"
}
Is there a way to do JSON response like this:{
"id": "4fba097e80db2609886ce7f2",
"titleFull": "Foo, LLC",
"titleShort": "Foo",
"countries": ["4fba04ef80db260988f8b607","4fba05f880db260988cd5cfd"],
"type": "company"
}public class Company
{
static Company()
{
BsonClassMap.RegisterClassMap<Company>( cm => { cm.AutoMap(); cm.MapMemeber(c => c.countries) //.GetMemberMap(c => c.countries) .SetSerializationOptions( new ArraySerializationOptions( new RepresentationSerializationOptions(BsonType.String))); //(BsonType.ObjectId))); }); }
[BsonRepresentation(BsonType.ObjectId)]
public String id { get; set; }
public String titleFull { get; set; }
public String titleShort { get; set; }
public String[] countries { get; set; }
//public ObjectId[] countries { get; set; }
public String type { get; set; }
}Aren't you just running into the issue in the JIRA I created?
Your exception is probably something like this, right?System.IO.FileFormatException: An error occurred while deserializing the X field of class ConsoleApplication1.C: Cannot deserialize string from BsonType ObjectId.
You're going to get the same exception whether you use an attribute on your array property or whether you use RegisterClassMap to set the ArraySerializationOptions until CSHARP-479 is fixed.
You're going to get the same exception whether you use an attribute on your array property or whether you use RegisterClassMap to set the ArraySerializationOptions until CSHARP-479 is fixed.