12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace HTEXLib.Helpers.ShapeHelpers
- {
- public static class JsonHelper
- {
- static JsonSerializerSettings settings = new JsonSerializerSettings()
- {
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
- PreserveReferencesHandling = PreserveReferencesHandling.None
- };
- /// <summary>
- /// 使用json序列化为字符串
- /// </summary>
- /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制,如:"\/Date(1439335800000+0800)\/"</param>
- /// <returns></returns>
- public static string ToJson(this object input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true, bool isIndented = false)
- {
- settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include;
- if (!string.IsNullOrWhiteSpace(dateTimeFormat))
- {
- var jsonConverter = new List<JsonConverter>()
- {
- new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
- };
- settings.Converters = jsonConverter;
- }
- //no format
- var format = isIndented ? Newtonsoft.Json.Formatting.Indented : Formatting.None;
- var json = JsonConvert.SerializeObject(input, format, settings);
- return json;
- }
- /// <summary>
- /// 从序列化字符串里反序列化
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="input"></param>
- /// <param name="dateTimeFormat">默认null,即使用json.net默认的序列化机制</param>
- /// <returns></returns>
- public static T FromJson<T>(this string input, string dateTimeFormat = "yyyy-MM-dd HH:mm:ss", bool ignoreNullValue = true)
- {
- var settings = new JsonSerializerSettings()
- {
- ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
- PreserveReferencesHandling = PreserveReferencesHandling.Objects,
- };
- settings.NullValueHandling = ignoreNullValue ? Newtonsoft.Json.NullValueHandling.Ignore : NullValueHandling.Include;
- if (!string.IsNullOrWhiteSpace(dateTimeFormat))
- {
- var jsonConverter = new List<JsonConverter>()
- {
- new Newtonsoft.Json.Converters.IsoDateTimeConverter(){ DateTimeFormat = dateTimeFormat }//如: "yyyy-MM-dd HH:mm:ss"
- };
- settings.Converters = jsonConverter;
- }
- return JsonConvert.DeserializeObject<T>(input, settings);
- }
- }
- }
|