1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Added support for duplicate array indexes in System.Text.Json ArrayConverter

This commit is contained in:
Jkorf 2024-10-09 15:41:43 +02:00
parent 7239b9c289
commit 71ee263683

View File

@ -94,41 +94,44 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
if (reader.TokenType == JsonTokenType.EndArray) if (reader.TokenType == JsonTokenType.EndArray)
break; break;
var attribute = attributes.SingleOrDefault(a => a.ArrayProperty.Index == index); var indexAttributes = attributes.Where(a => a.ArrayProperty.Index == index);
if (attribute == null) if (!indexAttributes.Any())
{ {
index++; index++;
continue; continue;
} }
var targetType = attribute.TargetType; foreach (var attribute in indexAttributes)
object? value = null;
if (attribute.JsonConverterType != null)
{ {
// Has JsonConverter attribute var targetType = attribute.TargetType;
var options = new JsonSerializerOptions(); object? value = null;
options.Converters.Add((JsonConverter)Activator.CreateInstance(attribute.JsonConverterType)); if (attribute.JsonConverterType != null)
value = JsonDocument.ParseValue(ref reader).Deserialize(targetType, options);
}
else if (attribute.DefaultDeserialization)
{
// Use default deserialization
value = JsonDocument.ParseValue(ref reader).Deserialize(targetType);
}
else
{
value = reader.TokenType switch
{ {
JsonTokenType.Null => null, // Has JsonConverter attribute
JsonTokenType.False => false, var options = new JsonSerializerOptions();
JsonTokenType.True => true, options.Converters.Add((JsonConverter)Activator.CreateInstance(attribute.JsonConverterType));
JsonTokenType.String => reader.GetString(), value = JsonDocument.ParseValue(ref reader).Deserialize(targetType, options);
JsonTokenType.Number => reader.GetDecimal(), }
_ => throw new NotImplementedException($"Array deserialization of type {reader.TokenType} not supported"), else if (attribute.DefaultDeserialization)
}; {
} // Use default deserialization
value = JsonDocument.ParseValue(ref reader).Deserialize(targetType);
}
else
{
value = reader.TokenType switch
{
JsonTokenType.Null => null,
JsonTokenType.False => false,
JsonTokenType.True => true,
JsonTokenType.String => reader.GetString(),
JsonTokenType.Number => reader.GetDecimal(),
_ => throw new NotImplementedException($"Array deserialization of type {reader.TokenType} not supported"),
};
}
attribute.PropertyInfo.SetValue(result, value == null ? null : Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture)); attribute.PropertyInfo.SetValue(result, value == null ? null : Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture));
}
index++; index++;
} }