Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public class TestClass
[JsonConverter(typeof(SmartEnumValueConverter<TestEnumBoolean, bool>))]
public TestEnumBoolean Bool { get; set; }

[JsonConverter(typeof(SmartEnumValueConverter<TestEnumByte, byte>))]
public TestEnumByte Byte { get; set; }

[JsonConverter(typeof(SmartEnumValueConverter<TestEnumSByte, sbyte>))]
public TestEnumSByte SByte { get; set; }

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

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -67,6 +77,8 @@ public void DeserializesValue()
var obj = JsonSerializer.Deserialize<TestClass>(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);
Expand All @@ -83,6 +95,8 @@ public void DeserializesNullByDefault()
var obj = JsonSerializer.Deserialize<TestClass>(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();
Expand All @@ -109,6 +123,8 @@ public void DeserializeThrowsWhenNotFound()
new TheoryData<string, string>
{
{ @"{ ""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." },
Expand Down
14 changes: 14 additions & 0 deletions test/SmartEnum.SystemTextJson.UnitTests/TestEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ public sealed class TestEnumBoolean : SmartEnum<TestEnumBoolean, bool>
TestEnumBoolean(string name, bool value) : base(name, value) { }
}

public sealed class TestEnumByte : SmartEnum<TestEnumByte, byte>
{
public static readonly TestEnumByte Instance = new TestEnumByte(nameof(Instance), 1);

TestEnumByte(string name, byte value) : base(name, value) { }
}

public sealed class TestEnumSByte : SmartEnum<TestEnumSByte, sbyte>
{
public static readonly TestEnumSByte Instance = new TestEnumSByte(nameof(Instance), 1);

TestEnumSByte(string name, sbyte value) : base(name, value) { }
}

public sealed class TestEnumInt16 : SmartEnum<TestEnumInt16, short>
{
public static readonly TestEnumInt16 Instance = new TestEnumInt16(nameof(Instance), 1);
Expand Down