using Newtonsoft.Json;
using System;
using System.ComponentModel;
namespace JiaZhiQuan.Common.Response {
public sealed class NumberConverter : JsonConverter {
///
/// 转换成字符串的类型
///
private readonly NumberConverterShip? _ship;
private readonly string _format;
///
/// 大数据json序列化重写实例化
///
public NumberConverter() {
}
///
/// 大数据json序列化重写实例化
///
/// 转换成字符串的类型
public NumberConverter(NumberConverterShip ship, string format=null) {
_ship = ship;
_format = format;
}
///
///
/// 确定此实例是否可以转换指定的对象类型。
///
/// 对象的类型。
/// 如果此实例可以转换指定的对象类型,则为:true,否则为:false
public override bool CanConvert(Type objectType) {
var typecode = Type.GetTypeCode(objectType.Name.Equals("Nullable`1") ?
objectType.GetGenericArguments()[0] : objectType);
return typecode switch {
TypeCode.Decimal => _ship == null || (_ship & NumberConverterShip.Decimal) == NumberConverterShip.Decimal,
TypeCode.Double => _ship == null || (_ship & NumberConverterShip.Double) == NumberConverterShip.Double,
TypeCode.Int64 => _ship == null || (_ship & NumberConverterShip.Int64) == NumberConverterShip.Int64,
TypeCode.UInt64 => _ship == null || (_ship & NumberConverterShip.UInt64) == NumberConverterShip.UInt64,
TypeCode.Single => _ship == null || (_ship & NumberConverterShip.Single) == NumberConverterShip.Single,
_ => false,
};
}
///
///
/// 读取对象的JSON表示。
///
/// 从 中读取。
/// 对象的类型。
/// 正在读取的对象的现有值。
/// 调用的序列化器实例。
/// 对象值。
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
return reader.Value == null ? null: AsType(reader.Value.ToString(), objectType);
}
///
/// 字符串格式数据转其他类型数据
///
/// 输入的字符串
/// 目标格式
/// 转换结果
public static object AsType(string input, Type destinationType) {
try {
var converter = TypeDescriptor.GetConverter(destinationType);
if (converter.CanConvertFrom(typeof(string))) {
return converter.ConvertFrom(null, null, input);
}
converter = TypeDescriptor.GetConverter(typeof(string));
if (converter.CanConvertTo(destinationType)) {
return converter.ConvertTo(null, null, input, destinationType);
}
} catch {
return null;
}
return null;
}
///
///
/// 写入对象的JSON表示形式。
///
/// 要写入的 。
/// 要写入对象值
/// 调用的序列化器实例。
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
if (value == null) {
writer.WriteNull();
} else {
var objectType = value.GetType();
var typeCode = Type.GetTypeCode(objectType.Name.Equals("Nullable`1") ?
objectType.GetGenericArguments()[0] : objectType);
switch (typeCode) {
case TypeCode.Decimal:
writer.WriteValue(((decimal)value).ToString(_format ?? "f6"));
break;
case TypeCode.Double:
writer.WriteValue(((double)value).ToString(_format ?? "f4"));
break;
case TypeCode.Single:
writer.WriteValue(((float)value).ToString(_format ?? "f2"));
break;
case TypeCode.Int64:
var typedInt64Value = (long)value;
if (typedInt64Value < Int32.MaxValue && typedInt64Value > Int32.MinValue)
{
writer.WriteValue(typedInt64Value);
}
else
{
writer.WriteValue(value.ToString());
}
break;
case TypeCode.UInt64:
var typedUInt64Value = (ulong)value;
if (typedUInt64Value < UInt32.MaxValue && typedUInt64Value > UInt32.MinValue)
{
writer.WriteValue(typedUInt64Value);
}
else
{
writer.WriteValue(value.ToString());
}
break;
default:
writer.WriteValue(value);
break;
}
}
}
}
///
/// 转换成字符串的类型
///
[Flags]
public enum NumberConverterShip {
///
/// 长整数
///
Int64 = 1,
///
/// 无符号长整数
///
UInt64 = 2,
///
/// 浮点数
///
Single = 4,
///
/// 双精度浮点数
///
Double = 8,
///
/// 大数字
///
Decimal = 16
}
}