Skip to content

Commit 079c821

Browse files
committed
Allow deserializing lists with null in them. Should resolve pull request #286.
1 parent 759b407 commit 079c821

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ public void Can_Deserialize_Simple_Generic_List_of_Simple_Types()
7171
Assert.NotEmpty(output);
7272
}
7373

74+
[Fact]
75+
public void Can_Deserialize_Simple_Generic_List_of_Simple_Types_With_Nulls ()
76+
{
77+
const string content = "{\"users\":[\"johnsheehan\",\"jagregory\",null,\"drusellers\",\"structuremap\"]}";
78+
var json = new JsonDeserializer { RootElement = "users" };
79+
80+
var output = json.Deserialize<List<string>> (new RestResponse { Content = content });
81+
82+
Assert.NotEmpty (output);
83+
Assert.Equal (null, output[2]);
84+
Assert.Equal (5, output.Count);
85+
}
86+
7487
[Fact]
7588
public void Can_Deserialize_Simple_Generic_List_Given_Item_Without_Array ()
7689
{

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,22 @@ private IList BuildList(Type type, object parent)
209209
}
210210
else if (itemType == typeof (string))
211211
{
212+
if (element == null)
213+
{
214+
list.Add (null);
215+
continue;
216+
}
217+
212218
list.Add (element.ToString ());
213219
}
214220
else
215221
{
222+
if (element == null)
223+
{
224+
list.Add (null);
225+
continue;
226+
}
227+
216228
var item = CreateAndMap (itemType, element);
217229
list.Add (item);
218230
}

0 commit comments

Comments
 (0)