diff --git a/RestSharp.Tests/JsonTests.cs b/RestSharp.Tests/JsonTests.cs index cb2431c47..4d8ce03be 100644 --- a/RestSharp.Tests/JsonTests.cs +++ b/RestSharp.Tests/JsonTests.cs @@ -32,6 +32,16 @@ public class JsonTests private const string GuidString = "AC1FC4BC-087A-4242-B8EE-C53EBE9887A5"; + [Fact] + public void Can_Deserialize_Select_Tokens() + { + var data = File.ReadAllText(Path.Combine("SampleData", "jsonarray.txt")); + var response = new RestResponse { Content = data }; + var json = new JsonDeserializer(); + var output = json.Deserialize(response); + Assert.Equal(4, output.Count); + } + [Fact] public void Can_Deserialize_4sq_Json_With_Root_Element_Specified() { diff --git a/RestSharp.Tests/SampleClasses/twitter.cs b/RestSharp.Tests/SampleClasses/twitter.cs index ed9d8908c..ed997fc6c 100644 --- a/RestSharp.Tests/SampleClasses/twitter.cs +++ b/RestSharp.Tests/SampleClasses/twitter.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using RestSharp.Deserializers; namespace RestSharp.Tests.SampleClasses { @@ -57,4 +58,25 @@ public class user public class StatusList : List { } + + public class complexStatus + { + public bool truncated { get; set; } + public string created_at { get; set; } + public string source { get; set; } + public bool favorited { get; set; } + public string in_reply_to_user_id { get; set; } + public string in_reply_to_status_id { get; set; } + public string in_reply_to_screen_name { get; set; } + // ignore contributors for now + [DeserializeAs(Name="user.following")] + public bool follow { get; set; } + // ignore geo + public long id { get; set; } + public string text { get; set; } + } + + public class StatusComplexList : List + { + } } diff --git a/RestSharp/Deserializers/JsonDeserializer.cs b/RestSharp/Deserializers/JsonDeserializer.cs index f546a7fea..e1df80610 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -83,13 +83,19 @@ private void Map(object target, IDictionary data) { name = prop.Name; } - - var actualName = name.GetNameVariants(Culture).FirstOrDefault(n => data.ContainsKey(n)); - var value = actualName != null ? data[actualName] : null; - - if (value == null) continue; - - prop.SetValue(target, ConvertValue(type, value), null); + + var parts = name.Split('.'); + var currentData = data; + object value = null; + for (var i = 0; i < parts.Length; ++i) + { + var actualName = parts[i].GetNameVariants(Culture).FirstOrDefault(currentData.ContainsKey); + if (actualName == null) break; + if(i == parts.Length - 1) value = currentData[actualName]; + else currentData = (IDictionary)currentData[actualName]; + } + + if(value != null) prop.SetValue(target, ConvertValue(type, value), null); } } @@ -101,17 +107,16 @@ private IDictionary BuildDictionary(Type type, object parent) { var key = child.Key; object item = null; - if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(List<>)) + if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(List<>)) { - item = BuildList(valueType, child.Value); + item = BuildList(valueType, child.Value); } else { - item = ConvertValue(valueType, child.Value); + item = ConvertValue(valueType, child.Value); } dict.Add(key, item); } - return dict; }