成都创新互联公司主要从事网站制作、成都做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务甘井子,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108>using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Globalization;
namespace Common.Core.Utilities
{
/// /// JSON帮助类
/// public static class JSONHelper
{
#region 编码
/// /// 编码
/// /// 类型 /// 要转换的类型数据 /// json字符串 public static string Encode(T t)
{
return Encode(t, Formatting.None);
}
#endregion #region 编码
/// /// 编码
/// /// /// /// /// public static string Encode(T t, Formatting format)
{
IsoDateTimeConverter timeConverter= new IsoDateTimeConverter();
BigintConverter bigintConverter= new BigintConverter();
//这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式 timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
return JsonConvert.SerializeObject(t, format, timeConverter, bigintConverter);
}
#endregion #region 解码
/// /// 解码
/// /// 类型 /// json字符串 /// 类型数据 public static T Decode(string json)
{
BigintConverter bigintConverter= new BigintConverter();
return (T)JsonConvert.DeserializeObject(json, typeof(T), bigintConverter);
}
#endregion
}
#region Bigint转换成字符串
/// /// Bigint类型转换处理
/// public class BigintConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(System.Int64)
|| objectType == typeof(System.UInt64);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return 0;
}
else
{
IConvertible convertible= reader.Value as IConvertible;
if (objectType == typeof(System.Int64))
{
return convertible.ToInt64(CultureInfo.InvariantCulture);
}
else if (objectType == typeof(System.UInt64))
{
return convertible.ToUInt64(CultureInfo.InvariantCulture);
}
return 0;
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteValue("0");
}
else if (value is Int64 || value is UInt64)
{
writer.WriteValue(value.ToString());
}
else
{
throw new Exception("Expected Bigint value");
}
}
}
#endregion
}