Skip to content

Commit b990048

Browse files
committed
Merge pull request restsharp#498 from shaylevi2/master
SelectTokens support ("path.to.data") added (with tests)
2 parents dc71b18 + 58912be commit b990048

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

RestSharp.Tests/JsonTests.cs

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public class JsonTests
3232

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

35+
[Fact]
36+
public void Can_Deserialize_Select_Tokens()
37+
{
38+
var data = File.ReadAllText(Path.Combine("SampleData", "jsonarray.txt"));
39+
var response = new RestResponse { Content = data };
40+
var json = new JsonDeserializer();
41+
var output = json.Deserialize<StatusComplexList>(response);
42+
Assert.Equal(4, output.Count);
43+
}
44+
3545
[Fact]
3646
public void Can_Deserialize_4sq_Json_With_Root_Element_Specified()
3747
{

RestSharp.Tests/SampleClasses/twitter.cs

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using RestSharp.Deserializers;
56

67
namespace RestSharp.Tests.SampleClasses
78
{
@@ -57,4 +58,25 @@ public class user
5758
public class StatusList : List<status>
5859
{
5960
}
61+
62+
public class complexStatus
63+
{
64+
public bool truncated { get; set; }
65+
public string created_at { get; set; }
66+
public string source { get; set; }
67+
public bool favorited { get; set; }
68+
public string in_reply_to_user_id { get; set; }
69+
public string in_reply_to_status_id { get; set; }
70+
public string in_reply_to_screen_name { get; set; }
71+
// ignore contributors for now
72+
[DeserializeAs(Name="user.following")]
73+
public bool follow { get; set; }
74+
// ignore geo
75+
public long id { get; set; }
76+
public string text { get; set; }
77+
}
78+
79+
public class StatusComplexList : List<complexStatus>
80+
{
81+
}
6082
}

RestSharp/Deserializers/JsonDeserializer.cs

+16-11
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,19 @@ private void Map(object target, IDictionary<string, object> data)
8383
{
8484
name = prop.Name;
8585
}
86-
87-
var actualName = name.GetNameVariants(Culture).FirstOrDefault(n => data.ContainsKey(n));
88-
var value = actualName != null ? data[actualName] : null;
89-
90-
if (value == null) continue;
91-
92-
prop.SetValue(target, ConvertValue(type, value), null);
86+
87+
var parts = name.Split('.');
88+
var currentData = data;
89+
object value = null;
90+
for (var i = 0; i < parts.Length; ++i)
91+
{
92+
var actualName = parts[i].GetNameVariants(Culture).FirstOrDefault(currentData.ContainsKey);
93+
if (actualName == null) break;
94+
if(i == parts.Length - 1) value = currentData[actualName];
95+
else currentData = (IDictionary<string, object>)currentData[actualName];
96+
}
97+
98+
if(value != null) prop.SetValue(target, ConvertValue(type, value), null);
9399
}
94100
}
95101

@@ -101,17 +107,16 @@ private IDictionary BuildDictionary(Type type, object parent)
101107
{
102108
var key = child.Key;
103109
object item = null;
104-
if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(List<>))
110+
if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(List<>))
105111
{
106-
item = BuildList(valueType, child.Value);
112+
item = BuildList(valueType, child.Value);
107113
}
108114
else
109115
{
110-
item = ConvertValue(valueType, child.Value);
116+
item = ConvertValue(valueType, child.Value);
111117
}
112118
dict.Add(key, item);
113119
}
114-
115120
return dict;
116121
}
117122

0 commit comments

Comments
 (0)