Skip to content

Commit b15f42c

Browse files
committed
1 parent e1879d8 commit b15f42c

File tree

2 files changed

+46
-56
lines changed

2 files changed

+46
-56
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
// distributed under the License is distributed on an "AS IS" BASIS,
1313
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
// See the License for the specific language governing permissions and
15-
// limitations under the License.
15+
// limitations under the License.
1616

17-
#endregion
17+
#endregion License
1818

19+
using NUnit.Framework;
20+
using RestSharp.Deserializers;
21+
using RestSharp.Tests.SampleClasses;
1922
using System;
2023
using System.Collections;
2124
using System.Collections.Generic;
2225
using System.Globalization;
2326
using System.IO;
2427
using System.Linq;
25-
using NUnit.Framework;
26-
using RestSharp.Deserializers;
27-
using RestSharp.Tests.SampleClasses;
2828

2929
namespace RestSharp.Tests
3030
{
@@ -195,7 +195,7 @@ public void Can_Deserialize_List_of_Guid()
195195
data["Ids"] = new JsonArray { id1, id2 };
196196

197197
JsonDeserializer d = new JsonDeserializer();
198-
RestResponse response = new RestResponse { Content = data.ToString() };
198+
RestResponse response = new RestResponse { Content = data.ToString() };
199199
GuidList p = d.Deserialize<GuidList>(response);
200200

201201
Assert.AreEqual(2, p.Ids.Count);
@@ -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();
@@ -1013,4 +1024,4 @@ private static T GetPayLoad<T>(string fileName)
10131024
return d.Deserialize<T>(response);
10141025
}
10151026
}
1016-
}
1027+
}

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System;
1+
using RestSharp.Extensions;
2+
using System;
23
using System.Collections;
34
using System.Collections.Generic;
45
using System.Globalization;
56
using System.Linq;
67
using System.Reflection;
7-
using RestSharp.Extensions;
88

99
namespace RestSharp.Deserializers
1010
{
@@ -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)
@@ -89,7 +68,7 @@ private object Map(object target, IDictionary<string, object> data)
8968

9069
if (attributes.Length > 0)
9170
{
92-
DeserializeAsAttribute attribute = (DeserializeAsAttribute) attributes[0];
71+
DeserializeAsAttribute attribute = (DeserializeAsAttribute)attributes[0];
9372
name = attribute.Name;
9473
}
9574
else
@@ -117,7 +96,7 @@ private object Map(object target, IDictionary<string, object> data)
11796
}
11897
else
11998
{
120-
currentData = (IDictionary<string, object>) currentData[actualName];
99+
currentData = (IDictionary<string, object>)currentData[actualName];
121100
}
122101
}
123102

@@ -132,11 +111,11 @@ private object Map(object target, IDictionary<string, object> data)
132111

133112
private IDictionary BuildDictionary(Type type, object parent)
134113
{
135-
IDictionary dict = (IDictionary) Activator.CreateInstance(type);
114+
IDictionary dict = (IDictionary)Activator.CreateInstance(type);
136115
Type keyType = type.GetGenericArguments()[0];
137116
Type valueType = type.GetGenericArguments()[1];
138117

139-
foreach (KeyValuePair<string, object> child in (IDictionary<string, object>) parent)
118+
foreach (KeyValuePair<string, object> child in (IDictionary<string, object>)parent)
140119
{
141120
object key = keyType != typeof(string)
142121
? Convert.ChangeType(child.Key, keyType, CultureInfo.InvariantCulture)
@@ -161,15 +140,15 @@ private IDictionary BuildDictionary(Type type, object parent)
161140

162141
private IList BuildList(Type type, object parent)
163142
{
164-
IList list = (IList) Activator.CreateInstance(type);
143+
IList list = (IList)Activator.CreateInstance(type);
165144
Type listType = type.GetInterfaces()
166145
.First
167146
(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList<>));
168147
Type itemType = listType.GetGenericArguments()[0];
169148

170149
if (parent is IList)
171150
{
172-
foreach (object element in (IList) parent)
151+
foreach (object element in (IList)parent)
173152
{
174153
if (itemType.IsPrimitive)
175154
{
@@ -272,14 +251,14 @@ private object ConvertValue(Type type, object value)
272251

273252
if (type == typeof(DateTimeOffset))
274253
{
275-
return (DateTimeOffset) dt;
254+
return (DateTimeOffset)dt;
276255
}
277256
}
278257
else if (type == typeof(decimal))
279258
{
280259
if (value is double)
281260
{
282-
return (decimal) ((double) value);
261+
return (decimal)((double)value);
283262
}
284263

285264
if (stringValue.Contains("e"))
@@ -339,7 +318,7 @@ private object ConvertValue(Type type, object value)
339318
}
340319
else if (type == typeof(JsonObject))
341320
{
342-
// simplify JsonObject into a Dictionary<string, object>
321+
// simplify JsonObject into a Dictionary<string, object>
343322
return this.BuildDictionary(typeof(Dictionary<string, object>), value);
344323
}
345324
else
@@ -355,9 +334,9 @@ private object CreateAndMap(Type type, object element)
355334
{
356335
object instance = Activator.CreateInstance(type);
357336

358-
this.Map(instance, (IDictionary<string, object>) element);
337+
this.Map(instance, (IDictionary<string, object>)element);
359338

360339
return instance;
361340
}
362341
}
363-
}
342+
}

0 commit comments

Comments
 (0)