mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-04-05 17:37:54 +08:00
feat(core): 抽离部分公共 JsonConverter
This commit is contained in:
parent
df51fa720d
commit
bab4ad48d6
@ -358,18 +358,18 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
|
||||
/// 获取或设置特殊资质图片媒体文件标识 ID 列表。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("qualifications")]
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringTypedStringIListConverter))]
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.JsonTypedStringIListConverter))]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("qualifications")]
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.StringTypedStringIListConverter))]
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.JsonTypedStringIListConverter))]
|
||||
public IList<string>? QualificationPictureMediaIdList { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置补充材料媒体文件标识 ID 列表。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("business_addition_pics")]
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringTypedStringIListConverter))]
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.JsonTypedStringIListConverter))]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("business_addition_pics")]
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.StringTypedStringIListConverter))]
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.JsonTypedStringIListConverter))]
|
||||
public IList<string>? BusinessAdditionPictureMediaIdList { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
@ -85,9 +85,9 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Models
|
||||
/// 获取或设置指定支付方式列表。
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonProperty("trade_type")]
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringTypedStringArrayConverter))]
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.JsonTypedStringArrayConverter))]
|
||||
[System.Text.Json.Serialization.JsonPropertyName("trade_type")]
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.StringTypedStringArrayConverter))]
|
||||
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Converters.JsonTypedStringArrayConverter))]
|
||||
public string[]? TradeTypeList { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
@ -5,7 +5,7 @@ using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class StringTypedStringArrayConverter : JsonConverter<string[]?>
|
||||
public class JsonTypedStringArrayConverter : JsonConverter<string[]?>
|
||||
{
|
||||
public override bool CanRead
|
||||
{
|
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class JsonTypedStringIListConverter : JsonConverter<IList<string>?>
|
||||
{
|
||||
private readonly JsonConverter<List<string>?> _converter = new JsonTypedStringListConverter();
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override IList<string>? ReadJson(JsonReader reader, Type objectType, IList<string>? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
return _converter.ReadJson(reader, objectType, ConvertIListToList(existingValue), hasExistingValue, serializer);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, IList<string>? value, JsonSerializer serializer)
|
||||
{
|
||||
_converter.WriteJson(writer, ConvertIListToList(value), serializer);
|
||||
}
|
||||
|
||||
private List<string>? ConvertIListToList(IList<string>? src)
|
||||
{
|
||||
if (src == null)
|
||||
return null;
|
||||
|
||||
List<string>? dest = src as List<string>;
|
||||
if (dest != null)
|
||||
return dest;
|
||||
|
||||
return new List<string>(src);
|
||||
}
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class StringTypedStringListConverter : JsonConverter<List<string>?>
|
||||
public class JsonTypedStringListConverter : JsonConverter<List<string>?>
|
||||
{
|
||||
public override bool CanRead
|
||||
{
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
|
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class StringTypedInt32IListConverter : JsonConverter<IList<int>?>
|
||||
{
|
||||
private readonly JsonConverter<List<int>?> _converter = new StringTypedInt32ListConverter();
|
||||
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override IList<int>? ReadJson(JsonReader reader, Type objectType, IList<int>? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
return _converter.ReadJson(reader, objectType, ConvertIListToList(existingValue), hasExistingValue, serializer);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, IList<int>? value, JsonSerializer serializer)
|
||||
{
|
||||
_converter.WriteJson(writer, ConvertIListToList(value), serializer);
|
||||
}
|
||||
|
||||
private List<int>? ConvertIListToList(IList<int>? src)
|
||||
{
|
||||
if (src == null)
|
||||
return null;
|
||||
|
||||
List<int>? dest = src as List<int>;
|
||||
if (dest != null)
|
||||
return dest;
|
||||
|
||||
return new List<int>(src);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class StringTypedInt32ListConverter : JsonConverter<List<int>?>
|
||||
{
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override List<int>? ReadJson(JsonReader reader, Type objectType, List<int>? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
string? value = serializer.Deserialize<string>(reader);
|
||||
if (value == null)
|
||||
return existingValue;
|
||||
|
||||
try
|
||||
{
|
||||
return value.Split(',').Select(e => int.Parse(e.Trim())).ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JsonReaderException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
throw new JsonReaderException();
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, List<int>? value, JsonSerializer serializer)
|
||||
{
|
||||
if (value != null)
|
||||
writer.WriteValue(string.Join(",", value));
|
||||
else
|
||||
writer.WriteNull();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class StringTypedStringListConverter : JsonConverter<List<string>?>
|
||||
{
|
||||
public override bool CanRead
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override bool CanWrite
|
||||
{
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override List<string>? ReadJson(JsonReader reader, Type objectType, List<string>? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (reader.TokenType == JsonToken.String)
|
||||
{
|
||||
string? value = serializer.Deserialize<string>(reader);
|
||||
if (value == null)
|
||||
return existingValue;
|
||||
|
||||
return value.Split(',').ToList();
|
||||
}
|
||||
|
||||
throw new JsonReaderException();
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, List<string>? value, JsonSerializer serializer)
|
||||
{
|
||||
if (value != null)
|
||||
writer.WriteValue(string.Join(",", value));
|
||||
else
|
||||
writer.WriteNull();
|
||||
}
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace System.Text.Json.Converters
|
||||
{
|
||||
public class StringTypedStringArrayConverter : JsonConverter<string[]?>
|
||||
public class JsonTypedStringArrayConverter : JsonConverter<string[]?>
|
||||
{
|
||||
public override string[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace System.Text.Json.Converters
|
||||
{
|
||||
public class JsonTypedStringIListConverter : JsonConverter<IList<string>?>
|
||||
{
|
||||
private readonly JsonConverter<List<string>?> _converter = new JsonTypedStringListConverter();
|
||||
|
||||
public override IList<string>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return _converter.Read(ref reader, typeToConvert, options);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, IList<string>? value, JsonSerializerOptions options)
|
||||
{
|
||||
_converter.Write(writer, ConvertIListToList(value), options);
|
||||
}
|
||||
|
||||
private List<string>? ConvertIListToList(IList<string>? src)
|
||||
{
|
||||
if (src == null)
|
||||
return null;
|
||||
|
||||
List<string>? dest = src as List<string>;
|
||||
if (dest != null)
|
||||
return dest;
|
||||
|
||||
return new List<string>(src);
|
||||
}
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace System.Text.Json.Converters
|
||||
{
|
||||
public class StringTypedStringListConverter : JsonConverter<List<string>?>
|
||||
public class JsonTypedStringListConverter : JsonConverter<List<string>?>
|
||||
{
|
||||
public override List<string>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
|
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace System.Text.Json.Converters
|
||||
{
|
||||
public class NumberTypedStringConverter : JsonConverter<string?>
|
||||
{
|
||||
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (reader.TokenType == JsonTokenType.Number)
|
||||
{
|
||||
if (reader.TryGetInt64(out long valueAsInt64))
|
||||
return valueAsInt64.ToString();
|
||||
else if (reader.TryGetUInt64(out ulong valueAsUInt64))
|
||||
return valueAsUInt64.ToString();
|
||||
else if (reader.TryGetDouble(out double valueAsDouble))
|
||||
return valueAsDouble.ToString();
|
||||
else
|
||||
return reader.GetDecimal().ToString();
|
||||
}
|
||||
else if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
return reader.GetString();
|
||||
}
|
||||
|
||||
throw new JsonException();
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
if (long.TryParse(value, out long valueAsInt64))
|
||||
writer.WriteNumberValue(valueAsInt64);
|
||||
else if (ulong.TryParse(value, out ulong valueAsUInt64))
|
||||
writer.WriteNumberValue(valueAsUInt64);
|
||||
else if (double.TryParse(value, out double valueAsDouble))
|
||||
writer.WriteNumberValue(valueAsDouble);
|
||||
else
|
||||
writer.WriteStringValue(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
|
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace System.Text.Json.Converters
|
||||
{
|
||||
public class StringTypedInt32IListConverter : JsonConverter<IList<int>?>
|
||||
{
|
||||
private readonly JsonConverter<List<int>?> _converter = new StringTypedInt32ListConverter();
|
||||
|
||||
public override IList<int>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return _converter.Read(ref reader, typeToConvert, options);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, IList<int>? value, JsonSerializerOptions options)
|
||||
{
|
||||
_converter.Write(writer, ConvertIListToList(value), options);
|
||||
}
|
||||
|
||||
private List<int>? ConvertIListToList(IList<int>? src)
|
||||
{
|
||||
if (src == null)
|
||||
return null;
|
||||
|
||||
List<int>? dest = src as List<int>;
|
||||
if (dest != null)
|
||||
return dest;
|
||||
|
||||
return new List<int>(src);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace System.Text.Json.Converters
|
||||
{
|
||||
public class StringTypedInt32ListConverter : JsonConverter<List<int>?>
|
||||
{
|
||||
public override List<int>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
string? value = reader.GetString();
|
||||
if (value == null)
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
return value.Split(',').Select(e => int.Parse(e.Trim())).ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JsonException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
throw new JsonException();
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, List<int>? value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value != null)
|
||||
writer.WriteStringValue(string.Join(",", value));
|
||||
else
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
|
@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace System.Text.Json.Converters
|
||||
{
|
||||
public class StringTypedStringListConverter : JsonConverter<List<string>?>
|
||||
{
|
||||
public override List<string>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
string? value = reader.GetString();
|
||||
if (value == null)
|
||||
return null;
|
||||
|
||||
return value.Split(',').ToList();
|
||||
}
|
||||
|
||||
throw new JsonException();
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, List<string>? value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value != null)
|
||||
writer.WriteStringValue(string.Join(",", value));
|
||||
else
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user