Use a customized JsonConverter to handle deserilizatrion the MongoDb based datetime json
"Submitted" : { "$date" : 1413030537700 }
the converter:
public class MongoDateConverter : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(DateTime)); } public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer) { JObject jObj = JObject.Load(reader); string dateText = jObj["$date"].ToString(); long timeLong = long.Parse(dateText); DateTime unixEpoch = new DateTime(1970,1,DateTimeKind.Utc); return unixEpoch.AddMilliseconds(timeLong); } public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer) { serializer.Serialize(writer,value.ToString()); } }
Apply the converter on data model fields like below:
[JsonConverter(typeof(MongoDateConverter))] public DateTime Submitted { get; set; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。