.net core json序列化 long類型轉化成字符串


在需要序列化的字段上標記特性

 [JsonConverter(typeof(NumberConverter), NumberConverterShip.Int64)]

以下是實現類

using System;
using System.ComponentModel;
using System.Linq;
using Newtonsoft.Json;

namespace Holder.Framework.Common
{
/// <inheritdoc />
/// <summary>
/// 大數據json序列化重寫
/// </summary>
public sealed class NumberConverter : JsonConverter
{
/// <summary>
/// 轉換成字符串的類型
/// </summary>
private readonly NumberConverterShip _ship;

/// <summary>
/// 大數據json序列化重寫實例化
/// </summary>
public NumberConverter()
{
_ship = (NumberConverterShip)0xFF;
}

/// <summary>
/// 大數據json序列化重寫實例化
/// </summary>
/// <param name="ship">轉換成字符串的類型</param>
public NumberConverter(NumberConverterShip ship)
{
_ship = ship;
}

/// <inheritdoc />
/// <summary>
/// 確定此實例是否可以轉換指定的對象類型。
/// </summary>
/// <param name="objectType">對象的類型。</param>
/// <returns>如果此實例可以轉換指定的對象類型,則為:<c>true</c>,否則為:<c>false</c></returns>
public override bool CanConvert(Type objectType)
{
var typecode = Type.GetTypeCode(objectType.Name.Equals("Nullable`1") ? objectType.GetGenericArguments().First() : objectType);
switch (typecode)
{
case TypeCode.Decimal:
return (_ship & NumberConverterShip.Decimal) == NumberConverterShip.Decimal;
case TypeCode.Double:
return (_ship & NumberConverterShip.Double) == NumberConverterShip.Double;
case TypeCode.Int64:
return (_ship & NumberConverterShip.Int64) == NumberConverterShip.Int64;
case TypeCode.UInt64:
return (_ship & NumberConverterShip.UInt64) == NumberConverterShip.UInt64;
case TypeCode.Single:
return (_ship & NumberConverterShip.Single) == NumberConverterShip.Single;
default: return false;
}
}

/// <inheritdoc />
/// <summary>
/// 讀取對象的JSON表示。
/// </summary>
/// <param name="reader">從 <see cref="T:Newtonsoft.Json.JsonReader" /> 中讀取。</param>
/// <param name="objectType">對象的類型。</param>
/// <param name="existingValue">正在讀取的對象的現有值。</param>
/// <param name="serializer">調用的序列化器實例。</param>
/// <returns>對象值。</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return AsType(reader.Value.ToString(), objectType);
}

/// <summary>
/// 字符串格式數據轉其他類型數據
/// </summary>
/// <param name="input">輸入的字符串</param>
/// <param name="destinationType">目標格式</param>
/// <returns>轉換結果</returns>
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;
}

/// <inheritdoc />
/// <summary>
/// 寫入對象的JSON表示形式。
/// </summary>
/// <param name="writer">要寫入的 <see cref="T:Newtonsoft.Json.JsonWriter" /> 。</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 objectType = value.GetType();
var typeCode = Type.GetTypeCode(objectType.Name.Equals("Nullable`1") ? objectType.GetGenericArguments().First() : objectType);
switch (typeCode)
{
case TypeCode.Decimal:
writer.WriteValue(((decimal)value).ToString("f6"));
break;
case TypeCode.Double:
writer.WriteValue(((double)value).ToString("f4"));
break;
case TypeCode.Single:
writer.WriteValue(((float)value).ToString("f2"));
break;
default:
writer.WriteValue(value.ToString());
break;
}
}
}
}

/// <summary>
/// 轉換成字符串的類型
/// </summary>
[Flags]
public enum NumberConverterShip
{
/// <summary>
/// 長整數
/// </summary>
Int64 = 1,

/// <summary>
/// 無符號長整數
/// </summary>
UInt64 = 2,

/// <summary>
/// 浮點數
/// </summary>
Single = 4,

/// <summary>
/// 雙精度浮點數
/// </summary>
Double = 8,

/// <summary>
/// 大數字
/// </summary>
Decimal = 16
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM