Skip to content

Commit b215a4e

Browse files
committed
Fixed #730
1 parent e1879d8 commit b215a4e

File tree

5 files changed

+55
-26
lines changed

5 files changed

+55
-26
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 19 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,18 @@ public void Can_Deserialize_Dictionary_of_Lists()
780780
Assert.IsNotEmpty(output.EmployeesPay);
781781
}
782782

783+
[Test]
784+
public void Can_Deserialize_Dictionary_with_Null()
785+
{
786+
string doc = File.ReadAllText(Path.Combine("SampleData", "jsondictionary_null.txt"));
787+
JsonDeserializer json = new JsonDeserializer { RootElement = "response" };
788+
IDictionary<string, object> output = json.Deserialize<Dictionary<string, object>>(new RestResponse { Content = doc });
789+
790+
IDictionary<string, object> dictionary = (IDictionary<string, object>)output["SomeDictionary"];
791+
Assert.AreEqual("abra", dictionary["NonNull"]);
792+
Assert.IsNull(dictionary["Null"]);
793+
}
794+
783795
private static string CreateJsonWithUnderscores()
784796
{
785797
JsonObject doc = new JsonObject();
@@ -1013,4 +1025,4 @@ private static T GetPayLoad<T>(string fileName)
10131025
return d.Deserialize<T>(response);
10141026
}
10151027
}
1016-
}
1028+
}

RestSharp.Tests/RestSharp.Tests.Signed.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@
142142
<Content Include="SampleData\jsondictionary.txt">
143143
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
144144
</Content>
145+
<Content Include="SampleData\jsondictionary_null.txt">
146+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
147+
</Content>
145148
<Content Include="SampleData\jsonenumtypes.txt">
146149
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
147150
</Content>

RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<Content Include="SampleData\iso8601datetimes.txt">
130130
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
131131
</Content>
132+
<Content Include="SampleData\jsondictionary_null.txt">
133+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
134+
</Content>
132135
<Content Include="SampleData\jsondictionary_KeysType.txt">
133136
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
134137
</Content>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"SomeDictionary" :
3+
{
4+
"NonNull": "abra",
5+
"Null": null
6+
}
7+
}

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 23 additions & 19 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
{
@@ -37,34 +37,34 @@ public T Deserialize<T>(IRestResponse response)
3737
{
3838
object root = this.FindRoot(response.Content);
3939

40-
target = (T) this.BuildList(objType, root);
40+
target = (T)this.BuildList(objType, root);
4141
}
4242
else
4343
{
4444
object data = SimpleJson.DeserializeObject(response.Content);
4545

46-
target = (T) this.BuildList(objType, data);
46+
target = (T)this.BuildList(objType, data);
4747
}
4848
}
4949
else if (target is IDictionary)
5050
{
5151
object root = this.FindRoot(response.Content);
5252

53-
target = (T) this.BuildDictionary(target.GetType(), root);
53+
target = (T)this.BuildDictionary(target.GetType(), root);
5454
}
5555
else
5656
{
5757
object root = this.FindRoot(response.Content);
5858

59-
target = (T) this.Map(target, (IDictionary<string, object>) root);
59+
target = (T)this.Map(target, (IDictionary<string, object>)root);
6060
}
6161

6262
return target;
6363
}
6464

6565
private object FindRoot(string content)
6666
{
67-
IDictionary<string, object> data = (IDictionary<string, object>) SimpleJson.DeserializeObject(content);
67+
IDictionary<string, object> data = (IDictionary<string, object>)SimpleJson.DeserializeObject(content);
6868

6969
if (this.RootElement.HasValue() && data.ContainsKey(this.RootElement))
7070
{
@@ -89,7 +89,7 @@ private object Map(object target, IDictionary<string, object> data)
8989

9090
if (attributes.Length > 0)
9191
{
92-
DeserializeAsAttribute attribute = (DeserializeAsAttribute) attributes[0];
92+
DeserializeAsAttribute attribute = (DeserializeAsAttribute)attributes[0];
9393
name = attribute.Name;
9494
}
9595
else
@@ -117,7 +117,7 @@ private object Map(object target, IDictionary<string, object> data)
117117
}
118118
else
119119
{
120-
currentData = (IDictionary<string, object>) currentData[actualName];
120+
currentData = (IDictionary<string, object>)currentData[actualName];
121121
}
122122
}
123123

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

133133
private IDictionary BuildDictionary(Type type, object parent)
134134
{
135-
IDictionary dict = (IDictionary) Activator.CreateInstance(type);
135+
IDictionary dict = (IDictionary)Activator.CreateInstance(type);
136136
Type keyType = type.GetGenericArguments()[0];
137137
Type valueType = type.GetGenericArguments()[1];
138138

139-
foreach (KeyValuePair<string, object> child in (IDictionary<string, object>) parent)
139+
foreach (KeyValuePair<string, object> child in (IDictionary<string, object>)parent)
140140
{
141141
object key = keyType != typeof(string)
142142
? Convert.ChangeType(child.Key, keyType, CultureInfo.InvariantCulture)
@@ -161,15 +161,15 @@ private IDictionary BuildDictionary(Type type, object parent)
161161

162162
private IList BuildList(Type type, object parent)
163163
{
164-
IList list = (IList) Activator.CreateInstance(type);
164+
IList list = (IList)Activator.CreateInstance(type);
165165
Type listType = type.GetInterfaces()
166166
.First
167167
(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList<>));
168168
Type itemType = listType.GetGenericArguments()[0];
169169

170170
if (parent is IList)
171171
{
172-
foreach (object element in (IList) parent)
172+
foreach (object element in (IList)parent)
173173
{
174174
if (itemType.IsPrimitive)
175175
{
@@ -225,8 +225,12 @@ private object ConvertValue(Type type, object value)
225225
type = type.GetGenericArguments()[0];
226226
}
227227

228-
if (type == typeof(object) && value != null)
228+
if (type == typeof(object))
229229
{
230+
if (value == null)
231+
{
232+
return null;
233+
}
230234
type = value.GetType();
231235
}
232236

@@ -272,14 +276,14 @@ private object ConvertValue(Type type, object value)
272276

273277
if (type == typeof(DateTimeOffset))
274278
{
275-
return (DateTimeOffset) dt;
279+
return (DateTimeOffset)dt;
276280
}
277281
}
278282
else if (type == typeof(decimal))
279283
{
280284
if (value is double)
281285
{
282-
return (decimal) ((double) value);
286+
return (decimal)((double)value);
283287
}
284288

285289
if (stringValue.Contains("e"))
@@ -339,7 +343,7 @@ private object ConvertValue(Type type, object value)
339343
}
340344
else if (type == typeof(JsonObject))
341345
{
342-
// simplify JsonObject into a Dictionary<string, object>
346+
// simplify JsonObject into a Dictionary<string, object>
343347
return this.BuildDictionary(typeof(Dictionary<string, object>), value);
344348
}
345349
else
@@ -355,9 +359,9 @@ private object CreateAndMap(Type type, object element)
355359
{
356360
object instance = Activator.CreateInstance(type);
357361

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

360364
return instance;
361365
}
362366
}
363-
}
367+
}

0 commit comments

Comments
 (0)