Handling MongoDB's ISODate() when attempting to parse a serialized JSON string Handling MongoDB's ISODate() when attempting to parse a serialized JSON string mongodb mongodb

Handling MongoDB's ISODate() when attempting to parse a serialized JSON string


I think you need to tweak your JSON serializer a bit more. Try this:

var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };Console.WriteLine(document.ToJson(jsonWriterSettings));


You SHOULD use JsonConvert to properly convert the Date to Json. the accepted answer produces a date object like: { $date: 190012312211 } as @Rick Strahl mentioned. This should work:

object val = BsonTypeMapper.MapToDotNetValue(bsonDocument);string jsonString = JsonConvert.SerializeObject(val);


If the JSON data is safe for eval (since its coming from your server it probably is) then you can do like so. It's not particularly pretty, but it gets the job done.

http://jsfiddle.net/CVLhx/

var str = '{"_id"  : ObjectId("52eaad4839b60812fca4bf28"),"Name": "Joe Blow","DateAdded" : ISODate("2014-01-30T19:51:35.977Z")}';function ObjectId(id) { return id;}function ISODate(d) {return d;}var obj = eval('(' + str + ')');console.log(obj);