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
6 changes: 5 additions & 1 deletion src/SmartEnum.JsonNet/SmartEnumValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existin
try
{
TValue value;
if (reader.TokenType == JsonToken.Integer && typeof(TValue) != typeof(long) && typeof(TValue) != typeof(bool))
if (reader.TokenType == JsonToken.String && typeof(TValue) == typeof(Guid))
{
value = (TValue)(object)Guid.Parse(reader.Value.ToString());
}
else if (reader.TokenType == JsonToken.Integer && typeof(TValue) != typeof(long) && typeof(TValue) != typeof(bool))
{
value = (TValue)Convert.ChangeType(reader.Value, typeof(TValue));
}
Expand Down
10 changes: 7 additions & 3 deletions src/SmartEnum.JsonNet/SmartFlagEnumValueConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using Newtonsoft.Json;
using System;

namespace Ardalis.SmartEnum.JsonNet;

Expand All @@ -10,7 +10,7 @@ namespace Ardalis.SmartEnum.JsonNet;
/// <typeparam name="TValue"></typeparam>
public class SmartFlagEnumValueConverter<TEnum, TValue> : JsonConverter<TEnum>
where TEnum : SmartFlagEnum<TEnum, TValue>
where TValue: struct, IEquatable<TValue>, IComparable<TValue>
where TValue : struct, IEquatable<TValue>, IComparable<TValue>
{
/// <summary>
/// Defaults to true.
Expand All @@ -36,7 +36,11 @@ public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existin
try
{
TValue value;
if (reader.TokenType == JsonToken.Integer && typeof(TValue) != typeof(long) && typeof(TValue) != typeof(bool))
if (reader.TokenType == JsonToken.String && typeof(TValue) == typeof(Guid))
{
value = (TValue)(object)Guid.Parse(reader.Value.ToString());
}
else if (reader.TokenType == JsonToken.Integer && typeof(TValue) != typeof(long) && typeof(TValue) != typeof(bool))
{
value = (TValue)Convert.ChangeType(reader.Value, typeof(TValue));
}
Expand Down
2 changes: 1 addition & 1 deletion src/SmartEnum.SystemTextJson/SmartEnumNameConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Ardalis.SmartEnum.SystemTextJson
/// <typeparam name="TValue"></typeparam>
public class SmartEnumNameConverter<TEnum, TValue> : JsonConverter<TEnum>
where TEnum : SmartEnum<TEnum, TValue>
where TValue : IEquatable<TValue>, IComparable<TValue>, IConvertible
where TValue : IEquatable<TValue>, IComparable<TValue>
{
/// <summary>
///
Expand Down
8 changes: 6 additions & 2 deletions src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Ardalis.SmartEnum.SystemTextJson
/// <typeparam name="TValue"></typeparam>
public class SmartEnumValueConverter<TEnum, TValue> : JsonConverter<TEnum>
where TEnum : SmartEnum<TEnum, TValue>
where TValue : IEquatable<TValue>, IComparable<TValue>, IConvertible
where TValue : IEquatable<TValue>, IComparable<TValue>
{
/// <summary>
///
Expand Down Expand Up @@ -41,7 +41,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 Down Expand Up @@ -98,6 +98,8 @@ private TValue ReadValue(ref Utf8JsonReader reader)
return (TValue)(object)reader.GetDouble();
if (typeof(TValue) == typeof(string))
return (TValue)(object)reader.GetString();
if (typeof(TValue) == typeof(Guid))
return (TValue)(object)Guid.Parse(reader.GetString());

throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported.");
}
Expand Down Expand Up @@ -155,6 +157,8 @@ private TValue ReadPropertyName(ref Utf8JsonReader reader)
return (TValue)(object)Convert.ToSingle(reader.GetString());
if (typeof(TValue) == typeof(double))
return (TValue)(object)Convert.ToDouble(reader.GetString());
if (typeof(TValue) == typeof(Guid))
return (TValue)(object)Guid.Parse(reader.GetString());
if (typeof(TValue) == typeof(string))
return (TValue)(object)reader.GetString();

Expand Down
6 changes: 5 additions & 1 deletion src/SmartEnum.Utf8Json/SmartEnumValueFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver
writer.WriteSingle((float)(object)value.Value);
else if (typeof(TValue) == typeof(double))
writer.WriteDouble((double)(object)value.Value);
else if (typeof(TValue) == typeof(Guid))
writer.WriteString(value.Value.ToString());
else
throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported.");
}
Expand All @@ -67,7 +69,7 @@ public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatter
return SmartEnum<TEnum, TValue>.FromValue(ReadValue(ref reader));
}

TValue ReadValue(ref JsonReader reader)
private TValue ReadValue(ref JsonReader reader)
{
if (typeof(TValue) == typeof(bool))
return (TValue)(object)reader.ReadBoolean();
Expand All @@ -91,6 +93,8 @@ TValue ReadValue(ref JsonReader reader)
return (TValue)(object)reader.ReadSingle();
if (typeof(TValue) == typeof(double))
return (TValue)(object)reader.ReadDouble();
if (typeof(TValue) == typeof(Guid))
return (TValue)(object)Guid.Parse(reader.ReadString());
throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported.");
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/SmartEnum.Utf8Json/SmartFlagEnumValueFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Ardalis.SmartEnum.Utf8Json
{
using global::Utf8Json;
using global::Utf8Json.Internal;
using System;

/// <summary>
Expand Down Expand Up @@ -46,6 +45,8 @@ public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver
writer.WriteSingle((float)(object)value.Value);
else if (typeof(TValue) == typeof(double))
writer.WriteDouble((double)(object)value.Value);
else if (typeof(TValue) == typeof(Guid))
writer.WriteString(value.Value.ToString());
else
throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported.");
}
Expand All @@ -64,7 +65,7 @@ public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatter
return SmartFlagEnum<TEnum, TValue>.DeserializeValue(ReadValue(ref reader));
}

TValue ReadValue(ref JsonReader reader)
private TValue ReadValue(ref JsonReader reader)
{
if (typeof(TValue) == typeof(byte))
return (TValue)(object)reader.ReadByte();
Expand All @@ -86,6 +87,8 @@ TValue ReadValue(ref JsonReader reader)
return (TValue)(object)reader.ReadSingle();
if (typeof(TValue) == typeof(double))
return (TValue)(object)reader.ReadDouble();
if (typeof(TValue) == typeof(Guid))
return (TValue)(object)Guid.Parse(reader.ReadString());
throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported.");
}
}
Expand Down
19 changes: 11 additions & 8 deletions test/SmartEnum.JsonNet.UnitTests/FlagTestEnums.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Ardalis.SmartEnum;
using System;

namespace Ardalis.SmartEnum.JsonNet.UnitTests
{
Expand All @@ -12,7 +8,7 @@ public sealed class FlagTestEnumInt16 : SmartFlagEnum<FlagTestEnumInt16, short>
{
public static readonly FlagTestEnumInt16 Instance = new FlagTestEnumInt16(nameof(Instance), 1);

FlagTestEnumInt16(string name, short value) : base(name, value) { }
private FlagTestEnumInt16(string name, short value) : base(name, value) { }
}

public sealed class FlagTestEnumInt32 : SmartFlagEnum<FlagTestEnumInt32, int>
Expand All @@ -21,14 +17,21 @@ public sealed class FlagTestEnumInt32 : SmartFlagEnum<FlagTestEnumInt32, int>
public static readonly FlagTestEnumInt32 Instance2 = new FlagTestEnumInt32(nameof(Instance2), 2);
public static readonly FlagTestEnumInt32 Instance3 = new FlagTestEnumInt32(nameof(Instance3), 4);

FlagTestEnumInt32(string name, int value) : base(name, value) { }
private FlagTestEnumInt32(string name, int value) : base(name, value) { }
}

public sealed class FlagTestEnumDouble : SmartFlagEnum<FlagTestEnumDouble, double>
{
public static readonly FlagTestEnumDouble Instance = new FlagTestEnumDouble(nameof(Instance), 1.0);

FlagTestEnumDouble(string name, double value) : base(name, value) { }
private FlagTestEnumDouble(string name, double value) : base(name, value) { }
}

public sealed class FlagTestEnumGuid : SmartFlagEnum<FlagTestEnumGuid, Guid>
{
public static readonly FlagTestEnumGuid Instance = new FlagTestEnumGuid(nameof(Instance), new Guid("00000000-1111-2222-3333-444444444444"));

private FlagTestEnumGuid(string name, Guid value) : base(name, value) { }
}
}
}
17 changes: 12 additions & 5 deletions test/SmartEnum.JsonNet.UnitTests/SmartEnumNameConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Ardalis.SmartEnum.JsonNet.UnitTests
{
using System;
using FluentAssertions;
using Newtonsoft.Json;
using System;
using Xunit;
using FluentAssertions;

public class SmartEnumNameConverterTests
{
Expand All @@ -23,24 +23,29 @@ public class TestClass

[JsonConverter(typeof(SmartEnumNameConverter<TestEnumString, string>))]
public TestEnumString String { get; set; }

[JsonConverter(typeof(SmartEnumNameConverter<TestEnumGuid, Guid>))]
public TestEnumGuid Guid { get; set; }
}

static readonly TestClass TestInstance = new TestClass
private static readonly TestClass TestInstance = new TestClass
{
Bool = TestEnumBoolean.Instance,
Int16 = TestEnumInt16.Instance,
Int32 = TestEnumInt32.Instance,
Double = TestEnumDouble.Instance,
String = TestEnumString.Instance,
Guid = TestEnumGuid.Instance,
};

static readonly string JsonString = JsonConvert.SerializeObject(new
private static readonly string JsonString = JsonConvert.SerializeObject(new
{
Bool = "Instance",
Int16 = "Instance",
Int32 = "Instance",
Double = "Instance",
String = "Instance"
String = "Instance",
Guid = "Instance"
}, Formatting.Indented);

[Fact]
Expand All @@ -61,6 +66,7 @@ public void DeserializesNames()
obj.Int32.Should().BeSameAs(TestEnumInt32.Instance);
obj.Double.Should().BeSameAs(TestEnumDouble.Instance);
obj.String.Should().BeSameAs(TestEnumString.Instance);
obj.Guid.Should().BeSameAs(TestEnumGuid.Instance);
}

[Fact]
Expand All @@ -75,6 +81,7 @@ public void DeserializesNullByDefault()
obj.Int32.Should().BeNull();
obj.Double.Should().BeNull();
obj.String.Should().BeNull();
obj.Guid.Should().BeNull();
}

[Fact]
Expand Down
18 changes: 13 additions & 5 deletions test/SmartEnum.JsonNet.UnitTests/SmartEnumValueConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Ardalis.SmartEnum.JsonNet.UnitTests
{
using System;
using FluentAssertions;
using Newtonsoft.Json;
using System;
using Xunit;
using FluentAssertions;

public class SmartEnumValueConverterTests
{
Expand All @@ -23,24 +23,29 @@ public class TestClass

[JsonConverter(typeof(SmartEnumValueConverter<TestEnumString, string>))]
public TestEnumString String { get; set; }

[JsonConverter(typeof(SmartEnumValueConverter<TestEnumGuid, Guid>))]
public TestEnumGuid Guid { get; set; }
}

static readonly TestClass TestInstance = new TestClass
private static readonly TestClass TestInstance = new TestClass
{
Bool = TestEnumBoolean.Instance,
Int16 = TestEnumInt16.Instance,
Int32 = TestEnumInt32.Instance,
Double = TestEnumDouble.Instance,
String = TestEnumString.Instance,
Guid = TestEnumGuid.Instance,
};

static readonly string JsonString = JsonConvert.SerializeObject(new
private static readonly string JsonString = JsonConvert.SerializeObject(new
{
Bool = true,
Int16 = 1,
Int32 = 1,
Double = 1.0,
String = "string"
String = "string",
Guid = "00000000-1111-2222-3333-444444444444"
}, Formatting.Indented);

[Fact]
Expand All @@ -61,6 +66,7 @@ public void DeserializesValue()
obj.Int32.Should().BeSameAs(TestEnumInt32.Instance);
obj.Double.Should().BeSameAs(TestEnumDouble.Instance);
obj.String.Should().BeSameAs(TestEnumString.Instance);
obj.Guid.Should().BeSameAs(TestEnumGuid.Instance);
}

[Fact]
Expand All @@ -75,6 +81,7 @@ public void DeserializesNullByDefault()
obj.Int32.Should().BeNull();
obj.Double.Should().BeNull();
obj.String.Should().BeNull();
obj.Guid.Should().BeNull();
}

[Fact]
Expand All @@ -99,6 +106,7 @@ public void DeserializeThrowsWhenNotFound()
{ @"{ ""Int32"": true }", "Error converting True to TestEnumInt32." },
{ @"{ ""Double"": true }", "Error converting True to TestEnumDouble." },
{ @"{ ""String"": true }", "Error converting True to TestEnumString." },
{ @"{ ""Guid"": true }", "Error converting True to TestEnumGuid." },
};

[Theory]
Expand Down
23 changes: 12 additions & 11 deletions test/SmartEnum.JsonNet.UnitTests/SmartFlagEnumNameConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ardalis.SmartEnum;
using Ardalis.SmartEnum.JsonNet;
using Ardalis.SmartEnum.JsonNet.UnitTests;
using FluentAssertions;
using Newtonsoft.Json;
using System;
using Xunit;

namespace Ardalis.SmartEnum.JsonNet.UnitTests
Expand All @@ -23,20 +17,25 @@ public class FlagTestClass

[JsonConverter(typeof(SmartFlagEnumNameConverter<FlagTestEnums.FlagTestEnumDouble, double>))]
public FlagTestEnums.FlagTestEnumDouble Double { get; set; }

[JsonConverter(typeof(SmartFlagEnumNameConverter<FlagTestEnums.FlagTestEnumGuid, Guid>))]
public FlagTestEnums.FlagTestEnumGuid Guid { get; set; }
}

static readonly FlagTestClass TestInstance = new FlagTestClass
private static readonly FlagTestClass TestInstance = new FlagTestClass
{
Int16 = FlagTestEnums.FlagTestEnumInt16.Instance,
Int32 = FlagTestEnums.FlagTestEnumInt32.Instance,
Double = FlagTestEnums.FlagTestEnumDouble.Instance
Double = FlagTestEnums.FlagTestEnumDouble.Instance,
Guid = FlagTestEnums.FlagTestEnumGuid.Instance
};

static readonly string JsonString = JsonConvert.SerializeObject(new
private static readonly string JsonString = JsonConvert.SerializeObject(new
{
Int16 = "Instance",
Int32 = "Instance",
Double = "Instance"
Double = "Instance",
Guid = "Instance"
}, Formatting.Indented);

[Fact]
Expand All @@ -55,6 +54,7 @@ public void DeserializesNames()
obj.Int16.Should().BeSameAs(FlagTestEnums.FlagTestEnumInt16.Instance);
obj.Int32.Should().BeSameAs(FlagTestEnums.FlagTestEnumInt32.Instance);
obj.Double.Should().BeSameAs(FlagTestEnums.FlagTestEnumDouble.Instance);
obj.Guid.Should().BeSameAs(FlagTestEnums.FlagTestEnumGuid.Instance);
}

[Fact]
Expand All @@ -67,6 +67,7 @@ public void DeserializesNullByDefault()
obj.Int16.Should().BeNull();
obj.Int32.Should().BeNull();
obj.Double.Should().BeNull();
obj.Guid.Should().BeNull();
}

[Fact]
Expand Down
Loading