JSON.NET cast error when serializing Mongo ObjectId

后端 未结 6 1252
旧巷少年郎
旧巷少年郎 2020-11-28 09:36

I am playing around with MongoDB and have an object with a mongodb ObjectId on it. When I serialise this with the .NET Json() method, all is good (but the dates are horrible

6条回答
  •  情话喂你
    2020-11-28 09:58

    I resolved a similar problem I was experiencing with the JSON.NET serializer/InvalidCastException error by setting the JsonOutputMode to strict, which eradicated the need to change the underlying type:

    var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
    var json = doc.ToJson(jsonWriterSettings);
    

    With further information available in the API: http://api.mongodb.org/csharp/1.8.3/html/d73bf108-d68c-e472-81af-36ac29ea08da.htm

提交回复
热议问题