Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions RestSharp.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StatusComplexList>(response);
Assert.Equal(4, output.Count);
}

[Fact]
public void Can_Deserialize_4sq_Json_With_Root_Element_Specified()
{
Expand Down
22 changes: 22 additions & 0 deletions RestSharp.Tests/SampleClasses/twitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestSharp.Deserializers;

namespace RestSharp.Tests.SampleClasses
{
Expand Down Expand Up @@ -57,4 +58,25 @@ public class user
public class StatusList : List<status>
{
}

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")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Out of curiosity, what happens if there happens to be a user property with a following property. I assume this supercedes that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the question correctly you mean something like this:

public class complexStatus
{
[DeserializeAs(Name="user.following")]
public bool follow { get; set; }

public User user { get; set; } // Contains a "following" field

}

Both will work.
The DeserializeAs is only relevant for the property it is assigned to.

public bool follow { get; set; }
// ignore geo
public long id { get; set; }
public string text { get; set; }
}

public class StatusComplexList : List<complexStatus>
{
}
}
27 changes: 16 additions & 11 deletions RestSharp/Deserializers/JsonDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,19 @@ private void Map(object target, IDictionary<string, object> 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<string, object>)currentData[actualName];
}

if(value != null) prop.SetValue(target, ConvertValue(type, value), null);
}
}

Expand All @@ -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;
}

Expand Down