diff --git a/src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs b/src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs index a2e9ee11..f0288a84 100644 --- a/src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs +++ b/src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs @@ -40,6 +40,10 @@ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOpt writer.WriteNullValue(); else if (typeof(TValue) == typeof(bool)) writer.WriteBooleanValue((bool)(object)value.Value); + else if (typeof(TValue) == typeof(byte)) + writer.WriteNumberValue((byte)(object)value.Value); + else if (typeof(TValue) == typeof(sbyte)) + writer.WriteNumberValue((sbyte)(object)value.Value); else if (typeof(TValue) == typeof(short)) writer.WriteNumberValue((int)(short)(object)value.Value); else if (typeof(TValue) == typeof(int)) diff --git a/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs b/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs index 22e093de..bc5ed8cb 100644 --- a/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs +++ b/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs @@ -96,6 +96,10 @@ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOpt writer.WriteNullValue(); else if (typeof(TValue) == typeof(bool)) writer.WriteBooleanValue((bool)(object)value.Value); + else if (typeof(TValue) == typeof(byte)) + writer.WriteNumberValue((byte)(object)value.Value); + else if (typeof(TValue) == typeof(sbyte)) + writer.WriteNumberValue((sbyte)(object)value.Value); else if (typeof(TValue) == typeof(short)) writer.WriteNumberValue((short)(object)value.Value); else if (typeof(TValue) == typeof(int)) diff --git a/test/SmartEnum.SystemTextJson.UnitTests/SmartEnumValueConverterTests.cs b/test/SmartEnum.SystemTextJson.UnitTests/SmartEnumValueConverterTests.cs index d5aa08dc..a50329d0 100644 --- a/test/SmartEnum.SystemTextJson.UnitTests/SmartEnumValueConverterTests.cs +++ b/test/SmartEnum.SystemTextJson.UnitTests/SmartEnumValueConverterTests.cs @@ -14,6 +14,12 @@ public class TestClass [JsonConverter(typeof(SmartEnumValueConverter))] public TestEnumBoolean Bool { get; set; } + [JsonConverter(typeof(SmartEnumValueConverter))] + public TestEnumByte Byte { get; set; } + + [JsonConverter(typeof(SmartEnumValueConverter))] + public TestEnumSByte SByte { get; set; } + [JsonConverter(typeof(SmartEnumValueConverter))] public TestEnumInt16 Int16 { get; set; } @@ -34,6 +40,8 @@ public class TestClass static readonly TestClass TestInstance = new TestClass { Bool = TestEnumBoolean.Instance, + Byte = TestEnumByte.Instance, + SByte = TestEnumSByte.Instance, Int16 = TestEnumInt16.Instance, Int32 = TestEnumInt32.Instance, Double = TestEnumDouble.Instance, @@ -45,6 +53,8 @@ public class TestClass static readonly string JsonString = JsonSerializer.Serialize(new { Bool = true, + Byte = 1, + SByte = 1, Int16 = 1, Int32 = 1, Double = 1.2, @@ -67,6 +77,8 @@ public void DeserializesValue() var obj = JsonSerializer.Deserialize(JsonString, TestJsonConverters.ValueConverterOptions); obj.Bool.Should().BeSameAs(TestEnumBoolean.Instance); + obj.Byte.Should().BeSameAs(TestEnumByte.Instance); + obj.SByte.Should().BeSameAs(TestEnumSByte.Instance); obj.Int16.Should().BeSameAs(TestEnumInt16.Instance); obj.Int32.Should().BeSameAs(TestEnumInt32.Instance); obj.Double.Should().BeSameAs(TestEnumDouble.Instance); @@ -83,6 +95,8 @@ public void DeserializesNullByDefault() var obj = JsonSerializer.Deserialize(json); obj.Bool.Should().BeNull(); + obj.Byte.Should().BeNull(); + obj.SByte.Should().BeNull(); obj.Int16.Should().BeNull(); obj.Int32.Should().BeNull(); obj.Double.Should().BeNull(); @@ -109,6 +123,8 @@ public void DeserializeThrowsWhenNotFound() new TheoryData { { @"{ ""Bool"": 1 }", @"Cannot get the value of a token type 'Number' as a boolean." }, + { @"{ ""Byte"": true }", @"Cannot get the value of a token type 'True' as a number." }, + { @"{ ""SByte"": true }", @"Cannot get the value of a token type 'True' as a number." }, { @"{ ""Int16"": true }", @"Cannot get the value of a token type 'True' as a number." }, { @"{ ""Int32"": true }", @"Cannot get the value of a token type 'True' as a number." }, { @"{ ""Double"": true }", @"Cannot get the value of a token type 'True' as a number." }, diff --git a/test/SmartEnum.SystemTextJson.UnitTests/TestEnums.cs b/test/SmartEnum.SystemTextJson.UnitTests/TestEnums.cs index 7f30514d..91bb71c0 100644 --- a/test/SmartEnum.SystemTextJson.UnitTests/TestEnums.cs +++ b/test/SmartEnum.SystemTextJson.UnitTests/TestEnums.cs @@ -11,6 +11,20 @@ public sealed class TestEnumBoolean : SmartEnum TestEnumBoolean(string name, bool value) : base(name, value) { } } + public sealed class TestEnumByte : SmartEnum + { + public static readonly TestEnumByte Instance = new TestEnumByte(nameof(Instance), 1); + + TestEnumByte(string name, byte value) : base(name, value) { } + } + + public sealed class TestEnumSByte : SmartEnum + { + public static readonly TestEnumSByte Instance = new TestEnumSByte(nameof(Instance), 1); + + TestEnumSByte(string name, sbyte value) : base(name, value) { } + } + public sealed class TestEnumInt16 : SmartEnum { public static readonly TestEnumInt16 Instance = new TestEnumInt16(nameof(Instance), 1);