diff --git a/CryptoExchange.Net/Clients/RestApiClient.cs b/CryptoExchange.Net/Clients/RestApiClient.cs index c1c91d5..6b894c5 100644 --- a/CryptoExchange.Net/Clients/RestApiClient.cs +++ b/CryptoExchange.Net/Clients/RestApiClient.cs @@ -15,7 +15,6 @@ using CryptoExchange.Net.Requests; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using static CryptoExchange.Net.Objects.RateLimiter; namespace CryptoExchange.Net { diff --git a/CryptoExchange.Net/Converters/BoolConverter.cs b/CryptoExchange.Net/Converters/BoolConverter.cs new file mode 100644 index 0000000..bb24714 --- /dev/null +++ b/CryptoExchange.Net/Converters/BoolConverter.cs @@ -0,0 +1,68 @@ +using System; +using Newtonsoft.Json; + +namespace CryptoExchange.Net.Converters +{ + /// + /// Boolean converter with support for "0"/"1" (strings) + /// + public class BoolConverter : JsonConverter + { + /// + /// Determines whether this instance can convert the specified object type. + /// + /// Type of the object. + /// + /// true if this instance can convert the specified object type; otherwise, false. + /// + public override bool CanConvert(Type objectType) + { + if (Nullable.GetUnderlyingType(objectType) != null) + return Nullable.GetUnderlyingType(objectType) == typeof(bool); + return objectType == typeof(bool); + } + + /// + /// Reads the JSON representation of the object. + /// + /// The to read from. + /// Type of the object. + /// The existing value of object being read. + /// The calling serializer. + /// + /// The object value. + /// + public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) + { + switch (reader.Value?.ToString().ToLower().Trim()) + { + case "true": + case "yes": + case "y": + case "1": + return true; + case "false": + case "no": + case "n": + case "0": + return false; + } + + // If we reach here, we're pretty much going to throw an error so let's let Json.NET throw it's pretty-fied error message. + return new JsonSerializer().Deserialize(reader, objectType); + } + + /// + /// Specifies that this converter will not participate in writing results. + /// + public override bool CanWrite { get { return false; } } + + /// + /// Writes the JSON representation of the object. + /// + /// The to write to.The value.The calling serializer. + public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) + { + } + } +} \ No newline at end of file diff --git a/CryptoExchange.Net/Converters/EnumConverter.cs b/CryptoExchange.Net/Converters/EnumConverter.cs index dff2251..1da595a 100644 --- a/CryptoExchange.Net/Converters/EnumConverter.cs +++ b/CryptoExchange.Net/Converters/EnumConverter.cs @@ -14,6 +14,20 @@ namespace CryptoExchange.Net.Converters /// public class EnumConverter : JsonConverter { + private bool _warnOnMissingEntry = true; + + /// + /// + public EnumConverter() { } + + /// + /// + /// + public EnumConverter(bool warnOnMissingEntry) + { + _warnOnMissingEntry = warnOnMissingEntry; + } + private static readonly ConcurrentDictionary>> _mapping = new(); /// @@ -51,8 +65,12 @@ namespace CryptoExchange.Net.Converters Trace.WriteLine($"{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff} | Warning | Received empty string as enum value, but property type is not a nullable enum. EnumType: {enumType.Name}. If you think {enumType.Name} should be nullable please open an issue on the Github repo"); } else + { // We received an enum value but weren't able to parse it. - Trace.WriteLine($"{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff} | Warning | Cannot map enum value. EnumType: {enumType.Name}, Value: {reader.Value}, Known values: {string.Join(", ", mapping.Select(m => m.Value))}. If you think {reader.Value} should added please open an issue on the Github repo"); + if (_warnOnMissingEntry) + Trace.WriteLine($"{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff} | Warning | Cannot map enum value. EnumType: {enumType.Name}, Value: {reader.Value}, Known values: {string.Join(", ", mapping.Select(m => m.Value))}. If you think {reader.Value} should added please open an issue on the Github repo"); + } + return defaultValue; } diff --git a/CryptoExchange.Net/ExchangeHelpers.cs b/CryptoExchange.Net/ExchangeHelpers.cs index df89554..9ce9f1a 100644 --- a/CryptoExchange.Net/ExchangeHelpers.cs +++ b/CryptoExchange.Net/ExchangeHelpers.cs @@ -128,7 +128,6 @@ namespace CryptoExchange.Net return value / 1.000000000000000000000000000000000m; } - /// /// Generate a new unique id. The id is staticly stored so it is guarenteed to be unique ///