Skip to content

Commit 0ef81fd

Browse files
author
Michael Hallett
committed
merged in pr restsharp#737
2 parents fe1fa9e + 98863df commit 0ef81fd

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

RestSharp.Tests/JsonTests.cs

+12
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,18 @@ public void Can_Deserialize_Plain_Values()
791791
Assert.AreEqual(result, new Guid("c02bdd1e-cce3-4b9c-8473-165e6e93b92a"));
792792
}
793793

794+
[Test]
795+
public void Can_Deserialize_Dictionary_with_Null()
796+
{
797+
string doc = File.ReadAllText(Path.Combine("SampleData", "jsondictionary_null.txt"));
798+
JsonDeserializer json = new JsonDeserializer { RootElement = "response" };
799+
IDictionary<string, object> output = json.Deserialize<Dictionary<string, object>>(new RestResponse { Content = doc });
800+
801+
IDictionary<string, object> dictionary = (IDictionary<string, object>)output["SomeDictionary"];
802+
Assert.AreEqual("abra", dictionary["NonNull"]);
803+
Assert.IsNull(dictionary["Null"]);
804+
}
805+
794806
private static string CreateJsonWithUnderscores()
795807
{
796808
JsonObject doc = new JsonObject();

RestSharp.Tests/RestSharp.Tests.Signed.csproj

+3
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

+3
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>
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

+5-1
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,12 @@ private object ConvertValue(Type type, object value)
204204
type = type.GetGenericArguments()[0];
205205
}
206206

207-
if (type == typeof(object) && value != null)
207+
if (type == typeof(object))
208208
{
209+
if (value == null)
210+
{
211+
return null;
212+
}
209213
type = value.GetType();
210214
}
211215

0 commit comments

Comments
 (0)