Skip to content

Commit 7cf212a

Browse files
committed
Adding support for deserializing enums from integer representations
1 parent 8763a56 commit 7cf212a

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,23 @@ public void Can_Deserialize_Various_Enum_Values ()
245245
var json = new JsonDeserializer ();
246246
var output = json.Deserialize<JsonEnumsTestStructure>(response);
247247

248-
Assert.Equal (output.Upper, Disposition.Friendly);
249-
Assert.Equal (output.Lower, Disposition.Friendly);
250-
Assert.Equal (output.CamelCased, Disposition.SoSo);
251-
Assert.Equal (output.Underscores, Disposition.SoSo);
252-
Assert.Equal (output.LowerUnderscores, Disposition.SoSo);
253-
Assert.Equal (output.Dashes, Disposition.SoSo);
254-
Assert.Equal (output.LowerDashes, Disposition.SoSo);
248+
Assert.Equal(Disposition.Friendly,output.Upper);
249+
Assert.Equal(Disposition.Friendly,output.Lower);
250+
Assert.Equal(Disposition.SoSo,output.CamelCased);
251+
Assert.Equal(Disposition.SoSo,output.Underscores);
252+
Assert.Equal(Disposition.SoSo,output.LowerUnderscores);
253+
Assert.Equal(Disposition.SoSo,output.Dashes);
254+
Assert.Equal(Disposition.SoSo,output.LowerDashes);
255+
Assert.Equal(Disposition.SoSo,output.Integer);
256+
}
257+
258+
[Fact]
259+
public void Deserialization_Of_Undefined_Int_Value_Still_Throws_InvalidOperationException()
260+
{
261+
const string data = @"{ ""Integer"" : 1024 }";
262+
var response = new RestResponse { Content = data };
263+
var json = new JsonDeserializer ();
264+
Assert.Throws<InvalidOperationException>(() => json.Deserialize<JsonEnumsTestStructure>(response));
255265
}
256266

257267
[Fact]

RestSharp.Tests/SampleClasses/misc.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,6 @@ public class JsonEnumsTestStructure
192192
public Disposition LowerUnderscores { get; set; }
193193
public Disposition Dashes { get; set; }
194194
public Disposition LowerDashes { get; set; }
195+
public Disposition Integer { get; set; }
195196
}
196197
}

RestSharp.Tests/SampleData/jsonenums.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
"lower_underscores": "so_so",
77
"dashes": "So-So",
88
"lower_dashes": "so_so",
9+
"integer" : 1
910
}

RestSharp.Tests/SerializerTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ public void Can_serialize_simple_POCO() {
6868
Assert.Equal(expected.ToString(), doc.ToString());
6969
}
7070

71+
[Fact]
72+
public void Can_serialize_Enum()
73+
{
74+
var enumClass = new ClassWithEnum
75+
{
76+
Color = Color.Red
77+
};
78+
79+
var xml = new XmlSerializer();
80+
var doc = xml.Serialize(enumClass);
81+
82+
var expected = new XDocument();
83+
var root = new XElement("ClassWithEnum");
84+
root.Add( new XElement("Color", "Red") );
85+
expected.Add(root);
86+
87+
Assert.Equal( expected.ToString(), doc.ToString() );
88+
}
89+
7190
[Fact]
7291
public void Can_serialize_simple_POCO_With_DateFormat_Specified() {
7392
var poco = new Person {
@@ -194,6 +213,18 @@ private class Item
194213
public int Value { get; set; }
195214
}
196215

216+
private enum Color
217+
{
218+
Red,
219+
Blue,
220+
Green
221+
}
222+
223+
private class ClassWithEnum
224+
{
225+
public Color Color { get; set; }
226+
}
227+
197228
[SerializeAs(Name = "Person")]
198229
private class WackyPerson
199230
{

RestSharp/Extensions/ReflectionExtensions.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,22 @@ public static object ChangeType(this object source, Type newType, CultureInfo cu
9292
public static object FindEnumValue(this Type type, string value, CultureInfo culture)
9393
{
9494
#if FRAMEWORK
95-
return Enum.GetValues(type)
96-
.Cast<Enum>()
95+
var enumValues = Enum.GetValues( type ).Cast<Enum>().ToList();
96+
try
97+
{
98+
return enumValues
9799
.First(v => v.ToString().GetNameVariants(culture).Contains(value, StringComparer.Create(culture, true)));
100+
}
101+
catch (InvalidOperationException)
102+
{
103+
int enumValueAsInt;
104+
if(int.TryParse( value, out enumValueAsInt ) && Enum.IsDefined(type, enumValueAsInt))
105+
{
106+
return enumValues
107+
.First(v => Convert.ToInt32(v) == enumValueAsInt);
108+
}
109+
throw;
110+
}
98111
#else
99112
return Enum.Parse(type, value, true);
100113
#endif

0 commit comments

Comments
 (0)