Skip to content

Commit b58aa46

Browse files
sdepouwardalis
andauthored
449: Support JSON serialization of ushort SmartEnums (#487)
* Support JSON serialization of ushort SmartEnums * Re-implement change post-fix --------- Co-authored-by: Steve Smith <steve@kentsmiths.com>
1 parent a429eb6 commit b58aa46

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOpt
9797
else if (typeof(TValue) == typeof(bool))
9898
writer.WriteBooleanValue((bool)(object)value.Value);
9999
else if (typeof(TValue) == typeof(short))
100-
writer.WriteNumberValue((int)(short)(object)value.Value);
100+
writer.WriteNumberValue((short)(object)value.Value);
101101
else if (typeof(TValue) == typeof(int))
102102
writer.WriteNumberValue((int)(object)value.Value);
103103
else if (typeof(TValue) == typeof(double))
@@ -106,6 +106,8 @@ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOpt
106106
writer.WriteNumberValue((decimal)(object)value.Value);
107107
else if (typeof(TValue) == typeof(ulong))
108108
writer.WriteNumberValue((ulong)(object)value.Value);
109+
else if (typeof(TValue) == typeof(ushort))
110+
writer.WriteNumberValue((ushort)(object)value.Value);
109111
else if (typeof(TValue) == typeof(uint))
110112
writer.WriteNumberValue((uint)(object)value.Value);
111113
else if (typeof(TValue) == typeof(float))

test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
namespace Ardalis.SmartEnum.SystemTextJson.UnitTests
22
{
3+
public sealed class FlagTestEnumUnsignedInt16 : SmartFlagEnum<FlagTestEnumUnsignedInt16, ushort>
4+
{
5+
public static readonly FlagTestEnumUnsignedInt16 Instance = new(nameof(Instance), 12);
6+
7+
FlagTestEnumUnsignedInt16(string name, ushort value) : base(name, value) { }
8+
}
9+
310
public sealed class FlagTestEnumInt16 : SmartFlagEnum<FlagTestEnumInt16, short>
411
{
512
public static readonly FlagTestEnumInt16 Instance = new FlagTestEnumInt16(nameof(Instance), 1);

test/SmartEnum.SystemTextJson.UnitTests/SmartFlagEnumValueConverterTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Ardalis.SmartEnum;
21
using FluentAssertions;
32
using System;
43
using System.Text.Json;
@@ -11,6 +10,9 @@ public class SmartFlagEnumValueConverterTests
1110
{
1211
public class TestClass
1312
{
13+
[JsonConverter(typeof(SmartFlagEnumValueConverter<FlagTestEnumUnsignedInt16, ushort>))]
14+
public FlagTestEnumUnsignedInt16 UnsignedInt16 { get; set; }
15+
1416
[JsonConverter(typeof(SmartFlagEnumValueConverter<FlagTestEnumInt16, short>))]
1517
public FlagTestEnumInt16 Int16 { get; set; }
1618

@@ -21,15 +23,17 @@ public class TestClass
2123
public FlagTestEnumDouble Double { get; set; }
2224
}
2325

24-
static readonly TestClass TestInstance = new TestClass
26+
static readonly TestClass TestInstance = new()
2527
{
28+
UnsignedInt16 = FlagTestEnumUnsignedInt16.Instance,
2629
Int16 = FlagTestEnumInt16.Instance,
2730
Int32 = FlagTestEnumInt32.Instance2,
2831
Double = FlagTestEnumDouble.Instance
2932
};
3033

3134
static readonly string JsonString = JsonSerializer.Serialize(new
3235
{
36+
UnsignedInt16 = 12,
3337
Int16 = 1,
3438
Int32 = 2,
3539
Double = 1

0 commit comments

Comments
 (0)