mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-04-05 17:37:54 +08:00
refactor(tenpayv2): 优化代码
This commit is contained in:
parent
fa7476ce1a
commit
b9512a863e
@ -17,7 +17,7 @@ namespace Newtonsoft.Json.Converters
|
||||
public const string PROPERTY_NAME_NARRAY = "#n";
|
||||
}
|
||||
|
||||
internal abstract partial class FlattenNArrayObjectConverterBase<T> : JsonConverter<T?>
|
||||
internal abstract partial class FlattenNArrayObjectConverterBase<T> : JsonConverter
|
||||
where T : class, new()
|
||||
{
|
||||
private sealed class InnerTypedJsonPropertyInfo
|
||||
@ -59,7 +59,12 @@ namespace Newtonsoft.Json.Converters
|
||||
get { return true; }
|
||||
}
|
||||
|
||||
public override T? ReadJson(JsonReader reader, Type objectType, T? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType.IsClass && !objectType.IsAbstract && !objectType.IsInterface;
|
||||
}
|
||||
|
||||
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
{
|
||||
@ -117,7 +122,7 @@ namespace Newtonsoft.Json.Converters
|
||||
throw new JsonSerializationException();
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, T? value, JsonSerializer serializer)
|
||||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
|
@ -45,6 +45,11 @@ namespace System.Text.Json.Converters
|
||||
|
||||
private static readonly Hashtable _mappedTypeJsonProperties = new Hashtable();
|
||||
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
return typeToConvert.IsClass && !typeToConvert.IsAbstract && !typeToConvert.IsInterface;
|
||||
}
|
||||
|
||||
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
@ -130,12 +135,12 @@ namespace System.Text.Json.Converters
|
||||
{
|
||||
// TODO: 优化
|
||||
tmpOptions.Converters.Remove(typedJsonProperty.JsonConverter);
|
||||
writer.WritePropertyName(propertyName);
|
||||
writer.WritePropertyName(tmpOptions.PropertyNamingPolicy?.ConvertName(propertyName) ?? propertyName);
|
||||
writer.WriteStringValue(JsonSerializer.Serialize(propertyValue, tmpOptions));
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WritePropertyName(propertyName);
|
||||
writer.WritePropertyName(tmpOptions.PropertyNamingPolicy?.ConvertName(propertyName) ?? propertyName);
|
||||
writer.WriteRawValue(JsonSerializer.Serialize(propertyValue, tmpOptions), skipInputValidation: true);
|
||||
}
|
||||
}
|
||||
@ -155,13 +160,14 @@ namespace System.Text.Json.Converters
|
||||
JsonObject jSubObject = JsonSerializer.SerializeToNode(element, tmpOptions)!.AsObject();
|
||||
foreach (KeyValuePair<string, JsonNode?> jSubProperty in jSubObject)
|
||||
{
|
||||
writer.WritePropertyName(jSubProperty.Key.Replace(PROPERTY_WILDCARD_NARRAY_ELEMENT, i.ToString()));
|
||||
writer.WriteRawValue(jSubProperty.Value?.ToJsonString(tmpOptions)!, skipInputValidation: true);
|
||||
string subPropertyName = jSubProperty.Key.Replace(PROPERTY_WILDCARD_NARRAY_ELEMENT, i.ToString());
|
||||
JsonNode? subPropertyValue = jSubProperty.Value;
|
||||
writer.WritePropertyName(tmpOptions.PropertyNamingPolicy?.ConvertName(subPropertyName) ?? subPropertyName);
|
||||
writer.WriteRawValue(subPropertyValue?.ToJsonString(tmpOptions)!, skipInputValidation: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,10 @@ namespace SKIT.FlurlHttpClient.Wechat
|
||||
newtonsoftJsonSettings.CheckAdditionalContent = true;
|
||||
newtonsoftJsonSettings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Error;
|
||||
var newtonsoftJsonSerializer = new FlurlNewtonsoftJsonSerializer(newtonsoftJsonSettings);
|
||||
var systemTextJsonSerializer = new FlurlSystemTextJsonSerializer();
|
||||
|
||||
var systemTextJsonOptions = FlurlSystemTextJsonSerializer.GetDefaultSerializerOptions();
|
||||
systemTextJsonOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
|
||||
var systemTextJsonSerializer = new FlurlSystemTextJsonSerializer(systemTextJsonOptions);
|
||||
|
||||
try
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user