diff --git a/src/WWB.UnifyApi/Converter/LongToStringConverter.cs b/src/WWB.UnifyApi/Converter/LongToStringConverter.cs new file mode 100644 index 0000000..15432cf --- /dev/null +++ b/src/WWB.UnifyApi/Converter/LongToStringConverter.cs @@ -0,0 +1,50 @@ +using Newtonsoft.Json; +using System; + +namespace WWB.UnifyApi.Converter +{ + public class LongToStringConverter : JsonConverter + { + /// + /// 是否开启自定义反序列化,值为true时,反序列化时会走ReadJson方法,值为false时,不走ReadJson方法,而是默认的反序列化 + /// + public override bool CanRead => false; + + /// + /// 是否开启自定义序列化,值为true时,序列化时会走WriteJson方法,值为false时,不走WriteJson方法,而是默认的序列化 + /// 类型等于long时才转换 + /// + /// + /// + public override bool CanConvert(Type objectType) + { + return objectType == typeof(long) || objectType == typeof(long?); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, + JsonSerializer serializer) + { + throw new NotImplementedException("Unnecessary because CanRead is false.The type will skip the converter."); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + if (value == null) + { + writer.WriteNull(); + return; + } + + var cValue = value.ToString(); + //长度大于等于19位的ID才返回String + if (cValue != null && (cValue.Length >= 15 || writer.Path.ToLower().Contains("id"))) + { + writer.WriteValue(cValue); + } + else + { + writer.WriteValue(value); + } + } + } +} diff --git a/src/WWB.UnifyApi/WWB.UnifyApi.csproj b/src/WWB.UnifyApi/WWB.UnifyApi.csproj index 471d407..9ef3b41 100644 --- a/src/WWB.UnifyApi/WWB.UnifyApi.csproj +++ b/src/WWB.UnifyApi/WWB.UnifyApi.csproj @@ -1,9 +1,9 @@ - + netcoreapp3.1 WWB.UnifyApi - 1.0.1 + 1.0.2 my6521 NetCore3.1 MIT @@ -13,5 +13,6 @@ + \ No newline at end of file