Skip to content

Commit

Permalink
Fix ArrayConverter error for nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Jul 5, 2024
1 parent 630f85e commit 6951f31
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private class ArrayPropertyInfo
public ArrayPropertyAttribute ArrayProperty { get; set; } = null!;
public Type? JsonConverterType { get; set; }
public bool DefaultDeserialization { get; set; }
public Type TargetType { get; set; }

Check warning on line 35 in CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TargetType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 35 in CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TargetType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 35 in CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TargetType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 35 in CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'TargetType' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

private class ArrayConverterInner<T> : JsonConverter<T>
Expand Down Expand Up @@ -70,7 +71,8 @@ private static List<ArrayPropertyInfo> CacheTypeAttributes(Type type)
ArrayProperty = att,
PropertyInfo = property,
DefaultDeserialization = property.GetCustomAttribute<JsonConversionAttribute>() != null,
JsonConverterType = property.GetCustomAttribute<JsonConverterAttribute>()?.ConverterType
JsonConverterType = property.GetCustomAttribute<JsonConverterAttribute>()?.ConverterType,
TargetType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType
});
}

Expand All @@ -96,8 +98,7 @@ private static object ParseObject(ref Utf8JsonReader reader, object result, Type
if (attribute == null)
continue;

var targetType = attribute.PropertyInfo.PropertyType;

var targetType = attribute.TargetType;
object? value = null;
if (attribute.JsonConverterType != null)
{
Expand All @@ -124,7 +125,7 @@ private static object ParseObject(ref Utf8JsonReader reader, object result, Type
};
}

attribute.PropertyInfo.SetValue(result, value == null ? null : Convert.ChangeType(value, attribute.PropertyInfo.PropertyType, CultureInfo.InvariantCulture));
attribute.PropertyInfo.SetValue(result, value == null ? null : Convert.ChangeType(value, targetType, CultureInfo.InvariantCulture));

index++;
}
Expand Down

0 comments on commit 6951f31

Please sign in to comment.