1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 07:56:12 +00:00
This commit is contained in:
Jkorf 2025-03-11 08:44:14 +01:00
parent a0e2f78a6a
commit 9f6eb9f0d0
7 changed files with 51 additions and 14 deletions

View File

@ -134,14 +134,15 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
if (att == null)
continue;
var converterType = property.GetCustomAttribute<JsonConverterAttribute>()?.ConverterType ?? property.PropertyType.GetCustomAttribute<JsonConverterAttribute>()?.ConverterType;
var targetType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
var converterType = property.GetCustomAttribute<JsonConverterAttribute>()?.ConverterType ?? targetType.GetCustomAttribute<JsonConverterAttribute>()?.ConverterType;
attributes.Add(new ArrayPropertyInfo
{
ArrayProperty = att,
PropertyInfo = property,
DefaultDeserialization = property.GetCustomAttribute<CryptoExchange.Net.Attributes.JsonConversionAttribute>() != null,
JsonConverter = converterType == null ? null : (JsonConverter)Activator.CreateInstance(converterType)!,
TargetType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType
TargetType = targetType
});
}
@ -195,7 +196,8 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
_converterOptionsCache.TryAdd(attribute.JsonConverter, newOptions);
}
value = JsonDocument.ParseValue(ref reader).Deserialize(attribute.PropertyInfo.PropertyType, newOptions);
var doc = JsonDocument.ParseValue(ref reader);
value = doc.Deserialize(attribute.PropertyInfo.PropertyType, newOptions);
}
else if (attribute.DefaultDeserialization)
{

View File

@ -29,7 +29,11 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
/// </summary>
/// <param name="enumValue"></param>
/// <returns></returns>
#if NET5_0_OR_GREATER
public static string GetString<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields)] T>(T enumValue) where T : struct, Enum
#else
public static string GetString<T>(T enumValue) where T : struct, Enum
#endif
=> EnumConverter<T>.GetString(enumValue);
/// <summary>
@ -38,7 +42,11 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
/// <param name="enumValue"></param>
/// <returns></returns>
[return: NotNullIfNotNull("enumValue")]
#if NET5_0_OR_GREATER
public static string? GetString<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields)] T>(T? enumValue) where T : struct, Enum
#else
public static string? GetString<T>(T? enumValue) where T : struct, Enum
#endif
=> EnumConverter<T>.GetString(enumValue);
}

View File

@ -10,5 +10,14 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
[AttributeUsage(System.AttributeTargets.Class | AttributeTargets.Enum | System.AttributeTargets.Interface)]
public class SerializationModelAttribute : Attribute
{
/// <summary>
/// ctor
/// </summary>
public SerializationModelAttribute() { }
/// <summary>
/// ctor
/// </summary>
/// <param name="type"></param>
public SerializationModelAttribute(Type type) { }
}
}

View File

@ -9,12 +9,12 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
/// <inheritdoc />
public class SystemTextJsonMessageSerializer : IMessageSerializer
{
private readonly JsonSerializerContext _options;
private readonly JsonSerializerOptions _options;
/// <summary>
/// ctor
/// </summary>
public SystemTextJsonMessageSerializer(JsonSerializerContext options)
public SystemTextJsonMessageSerializer(JsonSerializerOptions options)
{
_options = options;
}
@ -24,6 +24,6 @@ namespace CryptoExchange.Net.Converters.SystemTextJson
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "Everything referenced in the loaded assembly is manually preserved, so it's safe")]
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL3050:RequiresUnreferencedCode", Justification = "Everything referenced in the loaded assembly is manually preserved, so it's safe")]
#endif
public string Serialize<T>(T message) => JsonSerializer.Serialize(message, SerializerOptions.WithConverters(_options));
public string Serialize<T>(T message) => JsonSerializer.Serialize(message, _options);
}
}

View File

@ -192,6 +192,26 @@ namespace CryptoExchange.Net
return new string(randomChars);
}
/// <summary>
/// Generate a long value
/// </summary>
/// <param name="maxLength">Max character length</param>
/// <returns></returns>
public static long RandomLong(int maxLength)
{
#if NETSTANDARD2_1_OR_GREATER || NET9_0_OR_GREATER
var value = RandomNumberGenerator.GetInt32(0, int.MaxValue);
#else
var random = new Random();
var value = random.Next(0, int.MaxValue);
#endif
var val = value.ToString();
if (val.Length > maxLength)
return int.Parse(val.Substring(0, maxLength));
else
return value;
}
/// <summary>
/// Generate a random string of specified length
/// </summary>

View File

@ -103,10 +103,6 @@ namespace CryptoExchange.Net.SharedApis
/// Required optional parameters in the request
/// </summary>
public List<ParameterDescription> RequiredOptionalParameters { get; set; } = new List<ParameterDescription>();
/// <summary>
/// Unsupported optional parameters in the request
/// </summary>
public List<ParameterDescription> UnsupportedOptionalParameters { get; set; } = new List<ParameterDescription>();
/// <summary>
/// ctor
@ -156,8 +152,6 @@ namespace CryptoExchange.Net.SharedApis
sb.AppendLine(RequestNotes);
if (RequiredOptionalParameters.Any())
sb.AppendLine($"Required optional parameters: {string.Join(", ", RequiredOptionalParameters.Select(x => x.ToString()))}");
if (UnsupportedOptionalParameters.Any())
sb.AppendLine($"Unsupported optional specific parameters: {string.Join(", ", UnsupportedOptionalParameters.Select(x => x.ToString()))}");
if (RequiredExchangeParameters.Any())
sb.AppendLine($"Required exchange specific parameters: {string.Join(", ", RequiredExchangeParameters.Select(x => x.ToString()))}");
if (OptionalExchangeParameters.Any())

View File

@ -16,9 +16,13 @@ namespace CryptoExchange.Net.SharedApis
/// </summary>
public DateTime? DeliveryTime { get; set; }
/// <summary>
/// Max leverage setting
/// Max short leverage setting
/// </summary>
public decimal? MaxLeverage { get; set; }
public decimal? MaxShortLeverage { get; set; }
/// <summary>
/// Max long leverage setting
/// </summary>
public decimal? MaxLongLeverage { get; set; }
/// <summary>
/// ctor