Newtonsoft.Json 10.0
大家都说ServiceStack.Text在反序列化上效率更高,本人开始也使用此,但是在datetime类型上Newtonsoft.Json更具有灵活性。
序列化:
1 public static string ToJson(this object obj) 2 { 3 var str = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings 4 { 5 NullValueHandling = NullValueHandling.Ignore, 6 DateFormatString = "yyyy-MM-dd HH:mm:ss", 7 ReferenceLoopHandling = ReferenceLoopHandling.Ignore//循环引用 8 }); 9 return str; 10 }
反序列化:
1 public static T FromJson<T>(this string json) where T : class 2 { 3 return JsonConvert.DeserializeObject(json, typeof (T), new IsoDateTimeConverter 4 { 5 DateTimeFormat = "yyyy-MM-dd HH:mm:ss" 6 }) as T; 7 }