From 3a06bf0129427197a49f83a77fdecf6af3fb854b Mon Sep 17 00:00:00 2001 From: shaylevi2 Date: Wed, 29 Jan 2014 02:21:08 +0200 Subject: [PATCH 1/2] Added support for select tokens using the '.' in the Name parameter of "DeserializeAs" attribute --- RestSharp.Tests/JsonTests.cs | 10 ++++++++++ RestSharp.Tests/SampleClasses/twitter.cs | 22 +++++++++++++++++++++ RestSharp/Deserializers/JsonDeserializer.cs | 17 ++++++++++------ 3 files changed, 43 insertions(+), 6 deletions(-) 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..0e90b72cc 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -84,12 +84,17 @@ 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); } } From 58912beee4d0fc49385546ce7585d0b78f3e9d67 Mon Sep 17 00:00:00 2001 From: shaylevi2 Date: Wed, 29 Jan 2014 02:29:40 +0200 Subject: [PATCH 2/2] Fixed indentations --- RestSharp/Deserializers/JsonDeserializer.cs | 32 ++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/RestSharp/Deserializers/JsonDeserializer.cs b/RestSharp/Deserializers/JsonDeserializer.cs index 0e90b72cc..e1df80610 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -83,18 +83,19 @@ private void Map(object target, IDictionary data) { name = prop.Name; } - - 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); + + 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); } } @@ -106,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; }