Skip to content

Commit 166c698

Browse files
author
Michael Hallett
committed
Merge branch 'darvids0n-master'
2 parents 5afeaa3 + 40f922f commit 166c698

File tree

7 files changed

+64
-17
lines changed

7 files changed

+64
-17
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ public class JsonTests
3131

3232
private const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5";
3333

34+
[Fact]
35+
public void Can_Deserialize_Into_Struct()
36+
{
37+
const string content = "{\"one\":\"oneOneOne\", \"two\":\"twoTwoTwo\", \"three\":3}";
38+
var json = new JsonDeserializer();
39+
var output = json.Deserialize<SimpleStruct>(new RestResponse { Content = content });
40+
41+
Assert.NotNull(output);
42+
Assert.Equal("oneOneOne", output.One);
43+
Assert.Equal("twoTwoTwo", output.Two);
44+
Assert.Equal(3, output.Three);
45+
}
46+
3447
[Fact]
3548
public void Can_Deserialize_Select_Tokens()
3649
{

RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="SampleClasses\EmployeeTracker.cs" />
8585
<Compile Include="SampleClasses\EnumTest.cs" />
8686
<Compile Include="SampleClasses\GoogleWeatherWithAttributes.cs" />
87+
<Compile Include="SampleClasses\Struct.cs" />
8788
<Compile Include="XmlAttributeDeserializerTests.cs" />
8889
<Compile Include="CultureChange.cs" />
8990
<Compile Include="JsonTests.cs" />

RestSharp.Tests/SampleClasses/EmployeeTracker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class EmployeeTracker
1010
/// Value: Messages sent to employee.
1111
/// </summary>
1212
public Dictionary<String, List<String>> EmployeesMail { get; set; }
13+
1314
/// <summary>
1415
/// Key: Employee name.
1516
/// Value: Hours worked this each week.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+

2+
namespace RestSharp.Tests.SampleClasses
3+
{
4+
public struct SimpleStruct
5+
{
6+
public string One { get; set; }
7+
8+
public string Two { get; set; }
9+
10+
public int Three { get; set; }
11+
}
12+
}

RestSharp.Tests/XmlDeserializerTests.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,26 @@ private string PathFor(string sampleFile)
3636
return Path.Combine(SampleDataPath, sampleFile);
3737
}
3838

39+
[Fact]
40+
public void Can_Deserialize_Into_Struct()
41+
{
42+
const string content = "<root><one>oneOneOne</one><two>twoTwoTwo</two><three>3</three></root>";
43+
var xml = new XmlDeserializer();
44+
var output = xml.Deserialize<SimpleStruct>(new RestResponse { Content = content });
45+
46+
Assert.NotNull(output);
47+
Assert.Equal("oneOneOne", output.One);
48+
Assert.Equal("twoTwoTwo", output.Two);
49+
Assert.Equal(3, output.Three);
50+
}
3951
[Fact]
4052
public void Can_Deserialize_Lists_of_Simple_Types()
4153
{
4254
var xmlpath = PathFor("xmllists.xml");
4355
var doc = XDocument.Load(xmlpath);
4456

4557
var xml = new XmlDeserializer();
46-
var output = xml.Deserialize<SimpleTypesListSample>(new RestResponse() { Content = doc.ToString() });
58+
var output = xml.Deserialize<SimpleTypesListSample>(new RestResponse { Content = doc.ToString() });
4759

4860
Assert.NotEmpty(output.Names);
4961
Assert.NotEmpty(output.Numbers);
@@ -216,7 +228,7 @@ public void Can_Deserialize_Elements_to_Nullable_Values()
216228
{
217229
var culture = CultureInfo.InvariantCulture;
218230
var doc = CreateXmlWithoutEmptyValues(culture);
219-
var xml = new XmlDeserializer() { Culture = culture };
231+
var xml = new XmlDeserializer { Culture = culture };
220232
var output = xml.Deserialize<NullableValues>(new RestResponse { Content = doc });
221233

222234
Assert.NotNull(output.Id);

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public T Deserialize<T>(IRestResponse response)
4949
else
5050
{
5151
var root = FindRoot(response.Content);
52-
Map(target, (IDictionary<string, object>)root);
52+
target = (T)Map(target, (IDictionary<string, object>)root);
5353
}
5454

5555
return target;
@@ -67,7 +67,7 @@ private object FindRoot(string content)
6767
return data;
6868
}
6969

70-
private void Map(object target, IDictionary<string, object> data)
70+
private object Map(object target, IDictionary<string, object> data)
7171
{
7272
var objType = target.GetType();
7373
var props = objType.GetProperties().Where(p => p.CanWrite).ToList();
@@ -108,6 +108,8 @@ private void Map(object target, IDictionary<string, object> data)
108108
if (value != null)
109109
prop.SetValue(target, ConvertValue(type, value), null);
110110
}
111+
112+
return target;
111113
}
112114

113115
private IDictionary BuildDictionary(Type type, object parent)
@@ -204,23 +206,27 @@ private object ConvertValue(Type type, object value)
204206
{
205207
return value.ChangeType(type, Culture);
206208
}
207-
else if (type.IsEnum)
209+
210+
if (type.IsEnum)
208211
{
209212
return type.FindEnumValue(stringValue, Culture);
210213
}
211-
else if (type == typeof(Uri))
214+
215+
if (type == typeof(Uri))
212216
{
213217
return new Uri(stringValue, UriKind.RelativeOrAbsolute);
214218
}
215-
else if (type == typeof(string))
219+
220+
if (type == typeof(string))
216221
{
217222
return stringValue;
218223
}
219-
else if (type == typeof(DateTime)
224+
225+
if (type == typeof(DateTime)
220226
#if !PocketPC
221-
|| type == typeof(DateTimeOffset)
227+
|| type == typeof(DateTimeOffset)
222228
#endif
223-
)
229+
)
224230
{
225231
DateTime dt;
226232

@@ -241,7 +247,8 @@ private object ConvertValue(Type type, object value)
241247
{
242248
return dt;
243249
}
244-
else if (type == typeof(DateTimeOffset))
250+
251+
if (type == typeof(DateTimeOffset))
245252
{
246253
return (DateTimeOffset)dt;
247254
}
@@ -270,7 +277,8 @@ private object ConvertValue(Type type, object value)
270277
{
271278
return BuildList(type, value);
272279
}
273-
else if (genericTypeDef == typeof(Dictionary<,>))
280+
281+
if (genericTypeDef == typeof(Dictionary<,>))
274282
{
275283
var keyType = type.GetGenericArguments()[0];
276284

RestSharp/Deserializers/XmlDeserializer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public virtual T Deserialize<T>(IRestResponse response)
7070
}
7171
else
7272
{
73-
Map(x, root);
73+
x = (T)Map(x, root);
7474
}
7575

7676
return x;
@@ -100,7 +100,7 @@ private void RemoveNamespace(XDocument xdoc)
100100
}
101101
}
102102

103-
protected virtual void Map(object x, XElement root)
103+
protected virtual object Map(object x, XElement root)
104104
{
105105
var objType = x.GetType();
106106
var props = objType.GetProperties();
@@ -281,6 +281,8 @@ protected virtual void Map(object x, XElement root)
281281
}
282282
}
283283
}
284+
285+
return x;
284286
}
285287

286288
private static bool TryGetFromString(string inputString, out object result, Type type)
@@ -325,9 +327,7 @@ private object HandleListDerivative(object x, XElement root, string propName, Ty
325327
}
326328

327329
var list = (IList)Activator.CreateInstance(type);
328-
329330
var elements = root.Descendants(t.Name.AsNamespaced(Namespace));
330-
331331
var name = t.Name;
332332

333333
if (!elements.Any())
@@ -441,7 +441,7 @@ protected virtual XElement GetElementByName(XElement root, XName name)
441441
}
442442

443443
// try looking for element that matches sanitized property name (Order by depth)
444-
var element =
444+
var element =
445445
root.Descendants()
446446
.OrderBy(d => d.Ancestors().Count())
447447
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName) ?? root.Descendants()

0 commit comments

Comments
 (0)