From 27d49a6093afe5907b2c450daf6182768921cc30 Mon Sep 17 00:00:00 2001 From: JKorf Date: Tue, 9 Jan 2024 18:36:20 +0100 Subject: [PATCH] Added support null and empty string values to BoolConverter --- .../ConverterTests.cs | 52 +++++++++++++++++++ .../Converters/BoolConverter.cs | 11 +++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/CryptoExchange.Net.UnitTests/ConverterTests.cs b/CryptoExchange.Net.UnitTests/ConverterTests.cs index 07f66f6..f1b6181 100644 --- a/CryptoExchange.Net.UnitTests/ConverterTests.cs +++ b/CryptoExchange.Net.UnitTests/ConverterTests.cs @@ -20,6 +20,8 @@ namespace CryptoExchange.Net.UnitTests [TestCase("1620777600000")] [TestCase("2021-05-12T00:00:00.000Z")] [TestCase("2021-05-12T00:00:00.000000000Z")] + [TestCase("0.000000", true)] + [TestCase("0", true)] [TestCase("", true)] [TestCase(" ", true)] public void TestDateTimeConverterString(string input, bool expectNull = false) @@ -162,6 +164,44 @@ namespace CryptoExchange.Net.UnitTests var output = JsonConvert.DeserializeObject($"{{ \"Value\": {val} }}"); Assert.AreEqual(output.Value, expected); } + + [TestCase("1", true)] + [TestCase("true", true)] + [TestCase("yes", true)] + [TestCase("y", true)] + [TestCase("on", true)] + [TestCase("-1", false)] + [TestCase("0", false)] + [TestCase("n", false)] + [TestCase("no", false)] + [TestCase("false", false)] + [TestCase("off", false)] + [TestCase("", null)] + public void TestBoolConverter(string value, bool? expected) + { + var val = value == null ? "null" : $"\"{value}\""; + var output = JsonConvert.DeserializeObject($"{{ \"Value\": {val} }}"); + Assert.AreEqual(output.Value, expected); + } + + [TestCase("1", true)] + [TestCase("true", true)] + [TestCase("yes", true)] + [TestCase("y", true)] + [TestCase("on", true)] + [TestCase("-1", false)] + [TestCase("0", false)] + [TestCase("n", false)] + [TestCase("no", false)] + [TestCase("false", false)] + [TestCase("off", false)] + [TestCase("", false)] + public void TestBoolConverterNotNullable(string value, bool expected) + { + var val = value == null ? "null" : $"\"{value}\""; + var output = JsonConvert.DeserializeObject($"{{ \"Value\": {val} }}"); + Assert.AreEqual(output.Value, expected); + } } public class TimeObject @@ -180,6 +220,18 @@ namespace CryptoExchange.Net.UnitTests public TestEnum Value { get; set; } } + public class BoolObject + { + [JsonConverter(typeof(BoolConverter))] + public bool? Value { get; set; } + } + + public class NotNullableBoolObject + { + [JsonConverter(typeof(BoolConverter))] + public bool Value { get; set; } + } + [JsonConverter(typeof(EnumConverter))] public enum TestEnum { diff --git a/CryptoExchange.Net/Converters/BoolConverter.cs b/CryptoExchange.Net/Converters/BoolConverter.cs index 92b679e..40476eb 100644 --- a/CryptoExchange.Net/Converters/BoolConverter.cs +++ b/CryptoExchange.Net/Converters/BoolConverter.cs @@ -34,7 +34,16 @@ namespace CryptoExchange.Net.Converters /// public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { - switch (reader.Value?.ToString().ToLower().Trim()) + var value = reader.Value?.ToString().ToLower().Trim(); + if (value == null || value == "") + { + if (Nullable.GetUnderlyingType(objectType) != null) + return null; + + return false; + } + + switch (value) { case "true": case "yes":