diff --git a/RestSharp.Tests/JsonTests.cs b/RestSharp.Tests/JsonTests.cs index 456af1992..cb2431c47 100644 --- a/RestSharp.Tests/JsonTests.cs +++ b/RestSharp.Tests/JsonTests.cs @@ -277,6 +277,24 @@ public void Can_Deserialize_Various_Enum_Values () Assert.Equal(Disposition.SoSo,output.Integer); } + [Fact] + public void Can_Deserialize_Various_Enum_Types() + { + var data = File.ReadAllText(Path.Combine("SampleData", "jsonenumtypes.txt")); + var response = new RestResponse {Content = data}; + var json = new JsonDeserializer(); + var output = json.Deserialize(response); + + Assert.Equal(ByteEnum.EnumMin, output.ByteEnumType); + Assert.Equal(SByteEnum.EnumMin, output.SByteEnumType); + Assert.Equal(ShortEnum.EnumMin, output.ShortEnumType); + Assert.Equal(UShortEnum.EnumMin, output.UShortEnumType); + Assert.Equal(IntEnum.EnumMin, output.IntEnumType); + Assert.Equal(UIntEnum.EnumMin, output.UIntEnumType); + Assert.Equal(LongEnum.EnumMin, output.LongEnumType); + Assert.Equal(ULongEnum.EnumMin, output.ULongEnumType); + } + [Fact] public void Deserialization_Of_Undefined_Int_Value_Returns_Enum_Default() { diff --git a/RestSharp.Tests/RestSharp.Tests.csproj b/RestSharp.Tests/RestSharp.Tests.csproj index dc506f201..7e011bd26 100644 --- a/RestSharp.Tests/RestSharp.Tests.csproj +++ b/RestSharp.Tests/RestSharp.Tests.csproj @@ -81,6 +81,7 @@ + @@ -124,6 +125,9 @@ Always + + Always + Always diff --git a/RestSharp.Tests/SampleClasses/EnumTest.cs b/RestSharp.Tests/SampleClasses/EnumTest.cs new file mode 100644 index 000000000..5017c894e --- /dev/null +++ b/RestSharp.Tests/SampleClasses/EnumTest.cs @@ -0,0 +1,62 @@ +namespace RestSharp.Tests.SampleClasses +{ + public enum ByteEnum : byte + { + EnumMin = 0, + EnumMax = 255 + } + + public enum SByteEnum : sbyte + { + EnumMin = -128, + EnumMax = 127 + } + + public enum ShortEnum : short + { + EnumMin = -32768, + EnumMax = 32767 + } + + public enum UShortEnum : ushort + { + EnumMin = 0, + EnumMax = 65535 + } + + public enum IntEnum : int + { + EnumMin = -2147483648, + EnumMax = 2147483647 + } + + public enum UIntEnum : uint + { + EnumMin = 0, + EnumMax = 4294967295 + } + + public enum LongEnum : long + { + EnumMin = -9223372036854775808, + EnumMax = 9223372036854775807 + } + + public enum ULongEnum : ulong + { + EnumMin = 0, + EnumMax = 18446744073709551615 + } + + public class JsonEnumTypesTestStructure + { + public ByteEnum ByteEnumType { get; set; } + public SByteEnum SByteEnumType { get; set; } + public ShortEnum ShortEnumType { get; set; } + public UShortEnum UShortEnumType { get; set; } + public IntEnum IntEnumType { get; set; } + public UIntEnum UIntEnumType { get; set; } + public LongEnum LongEnumType { get; set; } + public ULongEnum ULongEnumType { get; set; } + } +} \ No newline at end of file diff --git a/RestSharp.Tests/SampleData/jsonenumtypes.txt b/RestSharp.Tests/SampleData/jsonenumtypes.txt new file mode 100644 index 000000000..9397d85f3 --- /dev/null +++ b/RestSharp.Tests/SampleData/jsonenumtypes.txt @@ -0,0 +1,10 @@ +{ + "ByteEnumType": 0, + "SByteEnumType": -128, + "ShortEnumType": -32768, + "UShortEnumType": 0, + "IntEnumType": -2147483648, + "UIntEnumType": 0, + "LongEnumType": -9223372036854775808, + "ULongEnumType" : 0 +} \ No newline at end of file diff --git a/RestSharp/Extensions/ReflectionExtensions.cs b/RestSharp/Extensions/ReflectionExtensions.cs index 416beb071..ca7382975 100644 --- a/RestSharp/Extensions/ReflectionExtensions.cs +++ b/RestSharp/Extensions/ReflectionExtensions.cs @@ -98,10 +98,10 @@ public static object FindEnumValue(this Type type, string value, CultureInfo cul if (ret == null) { - int enumValueAsInt; - if (Int32.TryParse(value, out enumValueAsInt) && Enum.IsDefined(type, enumValueAsInt)) + var enumValueAsUnderlyingType = Convert.ChangeType(value, Enum.GetUnderlyingType(type), culture); + if (enumValueAsUnderlyingType != null && Enum.IsDefined(type, enumValueAsUnderlyingType)) { - ret = (Enum) Enum.ToObject(type, enumValueAsInt); + ret = (Enum) Enum.ToObject(type, enumValueAsUnderlyingType); } }