Skip to content
Merged
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
18 changes: 18 additions & 0 deletions RestSharp.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonEnumTypesTestStructure>(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()
{
Expand Down
4 changes: 4 additions & 0 deletions RestSharp.Tests/RestSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="SampleClasses\EmployeeTracker.cs" />
<Compile Include="SampleClasses\EnumTest.cs" />
<Compile Include="SampleClasses\GoogleWeatherWithAttributes.cs" />
<Compile Include="XmlAttributeDeserializerTests.cs" />
<Compile Include="CultureChange.cs" />
Expand Down Expand Up @@ -124,6 +125,9 @@
<Content Include="SampleData\jsondictionary.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="SampleData\jsonenumtypes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="SampleData\objectproperty.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
62 changes: 62 additions & 0 deletions RestSharp.Tests/SampleClasses/EnumTest.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
10 changes: 10 additions & 0 deletions RestSharp.Tests/SampleData/jsonenumtypes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"ByteEnumType": 0,
"SByteEnumType": -128,
"ShortEnumType": -32768,
"UShortEnumType": 0,
"IntEnumType": -2147483648,
"UIntEnumType": 0,
"LongEnumType": -9223372036854775808,
"ULongEnumType" : 0
}
6 changes: 3 additions & 3 deletions RestSharp/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down