Figured a solution, hope it will help someone.
Basically in the controller instead of returning MVC's JsonResult, I returned the Newtonsoft's JObject.
My class looks like this:
using MongoDB.Bson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class StubClass
{
[JsonConverter(typeof(ObjectIdConverter))]
public ObjectId Id { get; set; }
}
The JsonConverter class looks like this:
using MongoDB.Bson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class ObjectIdConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value.ToString());
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
return new ObjectId(token.ToObject<string>());
}
public override bool CanConvert(Type objectType)
{
return typeof(ObjectId).IsAssignableFrom(objectType);
//return true;
}
}
And the controller:
using MongoDB.Bson;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
[HttpGet]
public JObject Index()
{
StubClass c = new StubClass()
{
Id = ObjectId.GenerateNewId()
};
JObject jobj = JObject.FromObject(c);
return jobj;
}