Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

449: Support JSON serialization of ushort SmartEnums #487

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
Loading