Skip to content

Commit

Permalink
449: Support JSON serialization of ushort SmartEnums (#487)
Browse files Browse the repository at this point in the history
* Support JSON serialization of ushort SmartEnums

* Re-implement change post-fix

---------

Co-authored-by: Steve Smith <[email protected]>
  • Loading branch information
sdepouw and ardalis authored Jan 19, 2024
1 parent a429eb6 commit b58aa46
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
namespace Ardalis.SmartEnum.SystemTextJson.UnitTests
{
public sealed class FlagTestEnumUnsignedInt16 : SmartFlagEnum<FlagTestEnumUnsignedInt16, ushort>
{
public static readonly FlagTestEnumUnsignedInt16 Instance = new(nameof(Instance), 12);

FlagTestEnumUnsignedInt16(string name, ushort value) : base(name, value) { }
}

public sealed class FlagTestEnumInt16 : SmartFlagEnum<FlagTestEnumInt16, short>
{
public static readonly FlagTestEnumInt16 Instance = new FlagTestEnumInt16(nameof(Instance), 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Ardalis.SmartEnum;
using FluentAssertions;
using System;
using System.Text.Json;
Expand All @@ -11,6 +10,9 @@ public class SmartFlagEnumValueConverterTests
{
public class TestClass
{
[JsonConverter(typeof(SmartFlagEnumValueConverter<FlagTestEnumUnsignedInt16, ushort>))]
public FlagTestEnumUnsignedInt16 UnsignedInt16 { get; set; }

[JsonConverter(typeof(SmartFlagEnumValueConverter<FlagTestEnumInt16, short>))]
public FlagTestEnumInt16 Int16 { get; set; }

Expand All @@ -21,15 +23,17 @@ 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
};

static readonly string JsonString = JsonSerializer.Serialize(new
{
UnsignedInt16 = 12,
Int16 = 1,
Int32 = 2,
Double = 1
Expand Down

0 comments on commit b58aa46

Please sign in to comment.