Skip to content

Commit fe1fa9e

Browse files
author
Michael Hallett
committed
Merge branch 'fix-635' of git://github.com/DixonD-git/RestSharp
2 parents f42a860 + 18f7853 commit fe1fa9e

File tree

2 files changed

+30
-48
lines changed

2 files changed

+30
-48
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,17 @@ public void Can_Deserialize_Dictionary_of_Lists()
780780
Assert.IsNotEmpty(output.EmployeesPay);
781781
}
782782

783+
[Test]
784+
public void Can_Deserialize_Plain_Values()
785+
{
786+
const string json = "\"c02bdd1e-cce3-4b9c-8473-165e6e93b92a\"";
787+
RestResponse response = new RestResponse { Content = json };
788+
JsonDeserializer d = new JsonDeserializer();
789+
Guid result = d.Deserialize<Guid>(response);
790+
791+
Assert.AreEqual(result, new Guid("c02bdd1e-cce3-4b9c-8473-165e6e93b92a"));
792+
}
793+
783794
private static string CreateJsonWithUnderscores()
784795
{
785796
JsonObject doc = new JsonObject();

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,51 +27,30 @@ public JsonDeserializer()
2727

2828
public T Deserialize<T>(IRestResponse response)
2929
{
30-
T target = Activator.CreateInstance<T>();
30+
object json = this.FindRoot(response.Content);
3131

32-
if (target is IList)
33-
{
34-
Type objType = target.GetType();
35-
36-
if (this.RootElement.HasValue())
37-
{
38-
object root = this.FindRoot(response.Content);
39-
40-
target = (T) this.BuildList(objType, root);
41-
}
42-
else
43-
{
44-
object data = SimpleJson.DeserializeObject(response.Content);
45-
46-
target = (T) this.BuildList(objType, data);
47-
}
48-
}
49-
else if (target is IDictionary)
50-
{
51-
object root = this.FindRoot(response.Content);
52-
53-
target = (T) this.BuildDictionary(target.GetType(), root);
54-
}
55-
else
56-
{
57-
object root = this.FindRoot(response.Content);
58-
59-
target = (T) this.Map(target, (IDictionary<string, object>) root);
60-
}
61-
62-
return target;
32+
return (T)this.ConvertValue(typeof(T), json);
6333
}
6434

6535
private object FindRoot(string content)
6636
{
67-
IDictionary<string, object> data = (IDictionary<string, object>) SimpleJson.DeserializeObject(content);
37+
object json = SimpleJson.DeserializeObject(content);
6838

69-
if (this.RootElement.HasValue() && data.ContainsKey(this.RootElement))
39+
if (this.RootElement.HasValue())
7040
{
71-
return data[this.RootElement];
41+
IDictionary<string, object> dictionary = json as IDictionary<string, object>;
42+
43+
if (dictionary != null)
44+
{
45+
object result;
46+
if (dictionary.TryGetValue(this.RootElement, out result))
47+
{
48+
return result;
49+
}
50+
}
7251
}
7352

74-
return data;
53+
return json;
7554
}
7655

7756
private object Map(object target, IDictionary<string, object> data)
@@ -318,19 +297,11 @@ private object ConvertValue(Type type, object value)
318297

319298
if (genericTypeDef == typeof(Dictionary<,>))
320299
{
321-
Type keyType = type.GetGenericArguments()[0];
322-
323-
// only supports Dict<string, T>()
324-
if (keyType == typeof(string))
325-
{
326-
return this.BuildDictionary(type, value);
327-
}
328-
}
329-
else
330-
{
331-
// nested property classes
332-
return this.CreateAndMap(type, value);
300+
return this.BuildDictionary(type, value);
333301
}
302+
303+
// nested property classes
304+
return this.CreateAndMap(type, value);
334305
}
335306
else if (type.IsSubclassOfRawGeneric(typeof(List<>)))
336307
{

0 commit comments

Comments
 (0)