From 9f6eb9f0d0f50a5e981ecfec277b9da9dd23c074 Mon Sep 17 00:00:00 2001 From: Jkorf Date: Tue, 11 Mar 2025 08:44:14 +0100 Subject: [PATCH] wip --- .../SystemTextJson/ArrayConverter.cs | 8 +++++--- .../SystemTextJson/EnumConverter.cs | 8 ++++++++ .../SystemTextJson/SerializationModel.cs | 9 +++++++++ .../SystemTextJsonMessageSerializer.cs | 6 +++--- CryptoExchange.Net/ExchangeHelpers.cs | 20 +++++++++++++++++++ .../Options/Endpoints/EndpointOptions.cs | 6 ------ .../ResponseModels/SharedFuturesSymbol.cs | 8 ++++++-- 7 files changed, 51 insertions(+), 14 deletions(-) diff --git a/CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs b/CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs index a3b9fff..33d79ac 100644 --- a/CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs +++ b/CryptoExchange.Net/Converters/SystemTextJson/ArrayConverter.cs @@ -134,14 +134,15 @@ namespace CryptoExchange.Net.Converters.SystemTextJson if (att == null) continue; - var converterType = property.GetCustomAttribute()?.ConverterType ?? property.PropertyType.GetCustomAttribute()?.ConverterType; + var targetType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; + var converterType = property.GetCustomAttribute()?.ConverterType ?? targetType.GetCustomAttribute()?.ConverterType; attributes.Add(new ArrayPropertyInfo { ArrayProperty = att, PropertyInfo = property, DefaultDeserialization = property.GetCustomAttribute() != 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) { diff --git a/CryptoExchange.Net/Converters/SystemTextJson/EnumConverter.cs b/CryptoExchange.Net/Converters/SystemTextJson/EnumConverter.cs index 9c7a645..d543d5d 100644 --- a/CryptoExchange.Net/Converters/SystemTextJson/EnumConverter.cs +++ b/CryptoExchange.Net/Converters/SystemTextJson/EnumConverter.cs @@ -29,7 +29,11 @@ namespace CryptoExchange.Net.Converters.SystemTextJson /// /// /// +#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 enumValue) where T : struct, Enum +#endif => EnumConverter.GetString(enumValue); /// @@ -38,7 +42,11 @@ namespace CryptoExchange.Net.Converters.SystemTextJson /// /// [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? enumValue) where T : struct, Enum +#endif => EnumConverter.GetString(enumValue); } diff --git a/CryptoExchange.Net/Converters/SystemTextJson/SerializationModel.cs b/CryptoExchange.Net/Converters/SystemTextJson/SerializationModel.cs index 987d9c6..3c958ec 100644 --- a/CryptoExchange.Net/Converters/SystemTextJson/SerializationModel.cs +++ b/CryptoExchange.Net/Converters/SystemTextJson/SerializationModel.cs @@ -10,5 +10,14 @@ namespace CryptoExchange.Net.Converters.SystemTextJson [AttributeUsage(System.AttributeTargets.Class | AttributeTargets.Enum | System.AttributeTargets.Interface)] public class SerializationModelAttribute : Attribute { + /// + /// ctor + /// + public SerializationModelAttribute() { } + /// + /// ctor + /// + /// + public SerializationModelAttribute(Type type) { } } } diff --git a/CryptoExchange.Net/Converters/SystemTextJson/SystemTextJsonMessageSerializer.cs b/CryptoExchange.Net/Converters/SystemTextJson/SystemTextJsonMessageSerializer.cs index ab04034..cd654c2 100644 --- a/CryptoExchange.Net/Converters/SystemTextJson/SystemTextJsonMessageSerializer.cs +++ b/CryptoExchange.Net/Converters/SystemTextJson/SystemTextJsonMessageSerializer.cs @@ -9,12 +9,12 @@ namespace CryptoExchange.Net.Converters.SystemTextJson /// public class SystemTextJsonMessageSerializer : IMessageSerializer { - private readonly JsonSerializerContext _options; + private readonly JsonSerializerOptions _options; /// /// ctor /// - 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 message) => JsonSerializer.Serialize(message, SerializerOptions.WithConverters(_options)); + public string Serialize(T message) => JsonSerializer.Serialize(message, _options); } } diff --git a/CryptoExchange.Net/ExchangeHelpers.cs b/CryptoExchange.Net/ExchangeHelpers.cs index 97f2bac..bb41dda 100644 --- a/CryptoExchange.Net/ExchangeHelpers.cs +++ b/CryptoExchange.Net/ExchangeHelpers.cs @@ -192,6 +192,26 @@ namespace CryptoExchange.Net return new string(randomChars); } + /// + /// Generate a long value + /// + /// Max character length + /// + 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; + } + /// /// Generate a random string of specified length /// diff --git a/CryptoExchange.Net/SharedApis/Models/Options/Endpoints/EndpointOptions.cs b/CryptoExchange.Net/SharedApis/Models/Options/Endpoints/EndpointOptions.cs index b4b7abf..996b2ec 100644 --- a/CryptoExchange.Net/SharedApis/Models/Options/Endpoints/EndpointOptions.cs +++ b/CryptoExchange.Net/SharedApis/Models/Options/Endpoints/EndpointOptions.cs @@ -103,10 +103,6 @@ namespace CryptoExchange.Net.SharedApis /// Required optional parameters in the request /// public List RequiredOptionalParameters { get; set; } = new List(); - /// - /// Unsupported optional parameters in the request - /// - public List UnsupportedOptionalParameters { get; set; } = new List(); /// /// 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()) diff --git a/CryptoExchange.Net/SharedApis/ResponseModels/SharedFuturesSymbol.cs b/CryptoExchange.Net/SharedApis/ResponseModels/SharedFuturesSymbol.cs index 944bc6f..ed4e84e 100644 --- a/CryptoExchange.Net/SharedApis/ResponseModels/SharedFuturesSymbol.cs +++ b/CryptoExchange.Net/SharedApis/ResponseModels/SharedFuturesSymbol.cs @@ -16,9 +16,13 @@ namespace CryptoExchange.Net.SharedApis /// public DateTime? DeliveryTime { get; set; } /// - /// Max leverage setting + /// Max short leverage setting /// - public decimal? MaxLeverage { get; set; } + public decimal? MaxShortLeverage { get; set; } + /// + /// Max long leverage setting + /// + public decimal? MaxLongLeverage { get; set; } /// /// ctor