Skip to content

Commit ccdb5d1

Browse files
committed
Merge pull request #467 from arangas/enum-type-parsing
Deserialization for enums of any type
2 parents 9985f6c + b1670cd commit ccdb5d1

File tree

5 files changed

+97
-3
lines changed

5 files changed

+97
-3
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,24 @@ public void Can_Deserialize_Various_Enum_Values ()
277277
Assert.Equal(Disposition.SoSo,output.Integer);
278278
}
279279

280+
[Fact]
281+
public void Can_Deserialize_Various_Enum_Types()
282+
{
283+
var data = File.ReadAllText(Path.Combine("SampleData", "jsonenumtypes.txt"));
284+
var response = new RestResponse {Content = data};
285+
var json = new JsonDeserializer();
286+
var output = json.Deserialize<JsonEnumTypesTestStructure>(response);
287+
288+
Assert.Equal(ByteEnum.EnumMin, output.ByteEnumType);
289+
Assert.Equal(SByteEnum.EnumMin, output.SByteEnumType);
290+
Assert.Equal(ShortEnum.EnumMin, output.ShortEnumType);
291+
Assert.Equal(UShortEnum.EnumMin, output.UShortEnumType);
292+
Assert.Equal(IntEnum.EnumMin, output.IntEnumType);
293+
Assert.Equal(UIntEnum.EnumMin, output.UIntEnumType);
294+
Assert.Equal(LongEnum.EnumMin, output.LongEnumType);
295+
Assert.Equal(ULongEnum.EnumMin, output.ULongEnumType);
296+
}
297+
280298
[Fact]
281299
public void Deserialization_Of_Undefined_Int_Value_Returns_Enum_Default()
282300
{

RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
</ItemGroup>
8282
<ItemGroup>
8383
<Compile Include="SampleClasses\EmployeeTracker.cs" />
84+
<Compile Include="SampleClasses\EnumTest.cs" />
8485
<Compile Include="SampleClasses\GoogleWeatherWithAttributes.cs" />
8586
<Compile Include="XmlAttributeDeserializerTests.cs" />
8687
<Compile Include="CultureChange.cs" />
@@ -124,6 +125,9 @@
124125
<Content Include="SampleData\jsondictionary.txt">
125126
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
126127
</Content>
128+
<Content Include="SampleData\jsonenumtypes.txt">
129+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
130+
</Content>
127131
<Content Include="SampleData\objectproperty.txt">
128132
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
129133
</Content>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace RestSharp.Tests.SampleClasses
2+
{
3+
public enum ByteEnum : byte
4+
{
5+
EnumMin = 0,
6+
EnumMax = 255
7+
}
8+
9+
public enum SByteEnum : sbyte
10+
{
11+
EnumMin = -128,
12+
EnumMax = 127
13+
}
14+
15+
public enum ShortEnum : short
16+
{
17+
EnumMin = -32768,
18+
EnumMax = 32767
19+
}
20+
21+
public enum UShortEnum : ushort
22+
{
23+
EnumMin = 0,
24+
EnumMax = 65535
25+
}
26+
27+
public enum IntEnum : int
28+
{
29+
EnumMin = -2147483648,
30+
EnumMax = 2147483647
31+
}
32+
33+
public enum UIntEnum : uint
34+
{
35+
EnumMin = 0,
36+
EnumMax = 4294967295
37+
}
38+
39+
public enum LongEnum : long
40+
{
41+
EnumMin = -9223372036854775808,
42+
EnumMax = 9223372036854775807
43+
}
44+
45+
public enum ULongEnum : ulong
46+
{
47+
EnumMin = 0,
48+
EnumMax = 18446744073709551615
49+
}
50+
51+
public class JsonEnumTypesTestStructure
52+
{
53+
public ByteEnum ByteEnumType { get; set; }
54+
public SByteEnum SByteEnumType { get; set; }
55+
public ShortEnum ShortEnumType { get; set; }
56+
public UShortEnum UShortEnumType { get; set; }
57+
public IntEnum IntEnumType { get; set; }
58+
public UIntEnum UIntEnumType { get; set; }
59+
public LongEnum LongEnumType { get; set; }
60+
public ULongEnum ULongEnumType { get; set; }
61+
}
62+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"ByteEnumType": 0,
3+
"SByteEnumType": -128,
4+
"ShortEnumType": -32768,
5+
"UShortEnumType": 0,
6+
"IntEnumType": -2147483648,
7+
"UIntEnumType": 0,
8+
"LongEnumType": -9223372036854775808,
9+
"ULongEnumType" : 0
10+
}

RestSharp/Extensions/ReflectionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ public static object FindEnumValue(this Type type, string value, CultureInfo cul
9898

9999
if (ret == null)
100100
{
101-
int enumValueAsInt;
102-
if (Int32.TryParse(value, out enumValueAsInt) && Enum.IsDefined(type, enumValueAsInt))
101+
var enumValueAsUnderlyingType = Convert.ChangeType(value, Enum.GetUnderlyingType(type), culture);
102+
if (enumValueAsUnderlyingType != null && Enum.IsDefined(type, enumValueAsUnderlyingType))
103103
{
104-
ret = (Enum) Enum.ToObject(type, enumValueAsInt);
104+
ret = (Enum) Enum.ToObject(type, enumValueAsUnderlyingType);
105105
}
106106
}
107107

0 commit comments

Comments
 (0)