Newtonsoft.Json⾃定义序列化格式转化器public static class JsonHelper
{
static JsonHelper()
{
Newtonsoft.Json.JsonSerializerSettings setting = new Newtonsoft.Json.JsonSerializerSettings();
JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() =>
{
//⽇期类型默认格式化处理
setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
//空值处理
setting.NullValueHandling = NullValueHandling.Ignore;
//⾼级⽤法九中的Bool类型转换设置
//setting.Converters.Add(new BoolConvert("是,否"));
if (setting.Converters.FirstOrDefault(p=>p.GetType()==typeof(JsonCustomDoubleConvert))==null)
{
setting.Converters.Add(new JsonCustomDoubleConvert(3));
}
return setting;
});
}
public static String ToJsonStr<T>(this T obj) where T : class
{
if (obj == null)
return string.Empty;
return JsonConvert.SerializeObject(obj, Formatting.Indented);
}
public static T ToInstance<T>(this String jsonStr) where T : class
{
if (string.IsNullOrEmpty(jsonStr))
return null;
try
{
var instance = JsonConvert.DeserializeObject<T>(jsonStr);
return instance;
}
catch
{
return null;
}
}
}
///<summary>
///⾃定义数值类型序列化转换器(默认保留3位)
/
//</summary>
public class JsonCustomDoubleConvert : CustomCreationConverter<double>
{
///<summary>
///序列化后保留⼩数位数
///</summary>
public virtual int Digits { get;private set; }
///<summary>
/// .ctor
///</summary>
public JsonCustomDoubleConvert()
{
this.Digits = 3;
}
///<summary>
/// .ctor
///</summary>
///<param name="digits">序列化后保留⼩数位数</param>
public JsonCustomDoubleConvert(int digits)
{
this.Digits = digits;
}
/
//<summary>
///重载是否可写
///</summary>
public override bool CanWrite { get { return true; } }
///<summary>
///重载创建⽅法
///</summary>
///<param name="objectType"></param>
///<returns></returns>
public override double Create(Type objectType)
{
浏览器json格式化
return0.0;
}
///<summary>
///重载序列化⽅法
///</summary>
///<param name="writer"></param>
///<param name="value"></param>
///<param name="serializer"></param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
if (value == null)
{
writer.WriteNull();
}
else
{
var formatter = ((double)value).ToString("N" + Digits.ToString());
writer.WriteValue(formatter);
}
}
}
///<summary>
///⾃定义数值类型序列化转换器(⽆⼩数位)
/
//</summary>
public class JsonCustomDoubleWith0DigitsConvert : JsonCustomDoubleConvert
{
public override int Digits
{
get { return0; }
}
}
///<summary>
///⾃定义数值类型序列化转换器(保留1位)
///</summary>
public class JsonCustomDoubleWith1DigitsConvert : JsonCustomDoubleConvert
{
public override int Digits
{
get { return1; }
}
}
///<summary>
///⾃定义数值类型序列化转换器(保留2位)
///</summary>
public class JsonCustomDoubleWith2DigitsConvert : JsonCustomDoubleConvert
{
public override int Digits
{
get { return2; }
}
}
///<summary>
///⾃定义数值类型序列化转换器(保留3位)
///</summary>
public class JsonCustomDoubleWith3DigitsConvert : JsonCustomDoubleConvert
{
public override int Digits
{
get { return3; }
}
}
///<summary>
///⾃定义数值类型序列化转换器(保留4位)
///</summary>
public class JsonCustomDoubleWith4DigitsConvert : JsonCustomDoubleConvert
{
public override int Digits
{
get { return4; }
}
}
///<summary>
///⾃定义数值类型序列化转换器(保留5位)
///</summary>
public class JsonCustomDoubleWith5DigitsConvert : JsonCustomDoubleConvert {
public override int Digits
{
get { return5; }
}
}
class Order
{
public int Id { get; set; }
public static int OrderId = GetOrderId();
public IEnumerable<int> Missions = null;
static int GetOrderId()
{
return new Random().Next(1, 10000);
}
[JsonConverter(typeof(JsonCustomDoubleWith0DigitsConvert))] public double OrderAmount { get; set; }
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论