diff --git a/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs b/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs index 6c9e288d..22e093de 100644 --- a/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs +++ b/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs @@ -97,7 +97,7 @@ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOpt else if (typeof(TValue) == typeof(bool)) writer.WriteBooleanValue((bool)(object)value.Value); else if (typeof(TValue) == typeof(short)) - writer.WriteNumberValue((int)(short)(object)value.Value); + writer.WriteNumberValue((short)(object)value.Value); else if (typeof(TValue) == typeof(int)) writer.WriteNumberValue((int)(object)value.Value); else if (typeof(TValue) == typeof(double)) @@ -106,6 +106,8 @@ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOpt writer.WriteNumberValue((decimal)(object)value.Value); else if (typeof(TValue) == typeof(ulong)) writer.WriteNumberValue((ulong)(object)value.Value); + else if (typeof(TValue) == typeof(ushort)) + writer.WriteNumberValue((ushort)(object)value.Value); else if (typeof(TValue) == typeof(uint)) writer.WriteNumberValue((uint)(object)value.Value); else if (typeof(TValue) == typeof(float)) diff --git a/test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs b/test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs index 07c06fc0..25bafd2b 100644 --- a/test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs +++ b/test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs @@ -1,5 +1,12 @@ namespace Ardalis.SmartEnum.SystemTextJson.UnitTests { + public sealed class FlagTestEnumUnsignedInt16 : SmartFlagEnum + { + public static readonly FlagTestEnumUnsignedInt16 Instance = new(nameof(Instance), 12); + + FlagTestEnumUnsignedInt16(string name, ushort value) : base(name, value) { } + } + public sealed class FlagTestEnumInt16 : SmartFlagEnum { public static readonly FlagTestEnumInt16 Instance = new FlagTestEnumInt16(nameof(Instance), 1); diff --git a/test/SmartEnum.SystemTextJson.UnitTests/SmartFlagEnumValueConverterTests.cs b/test/SmartEnum.SystemTextJson.UnitTests/SmartFlagEnumValueConverterTests.cs index 8395d0f8..801bc582 100644 --- a/test/SmartEnum.SystemTextJson.UnitTests/SmartFlagEnumValueConverterTests.cs +++ b/test/SmartEnum.SystemTextJson.UnitTests/SmartFlagEnumValueConverterTests.cs @@ -1,4 +1,3 @@ -using Ardalis.SmartEnum; using FluentAssertions; using System; using System.Text.Json; @@ -11,6 +10,9 @@ public class SmartFlagEnumValueConverterTests { public class TestClass { + [JsonConverter(typeof(SmartFlagEnumValueConverter))] + public FlagTestEnumUnsignedInt16 UnsignedInt16 { get; set; } + [JsonConverter(typeof(SmartFlagEnumValueConverter))] public FlagTestEnumInt16 Int16 { get; set; } @@ -21,8 +23,9 @@ public class TestClass public FlagTestEnumDouble Double { get; set; } } - static readonly TestClass TestInstance = new TestClass + static readonly TestClass TestInstance = new() { + UnsignedInt16 = FlagTestEnumUnsignedInt16.Instance, Int16 = FlagTestEnumInt16.Instance, Int32 = FlagTestEnumInt32.Instance2, Double = FlagTestEnumDouble.Instance @@ -30,6 +33,7 @@ public class TestClass static readonly string JsonString = JsonSerializer.Serialize(new { + UnsignedInt16 = 12, Int16 = 1, Int32 = 2, Double = 1