Skip to content

Commit 19606c6

Browse files
committed
Merge pull request #292 from petejohanson/str_str_dictionary
Add support for mapping JSON objects to Dictionary<string, string> when ...
2 parents 9a1a1fd + 40e202d commit 19606c6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,20 @@ public void Can_Deserialize_To_Dictionary_String_String()
508508
Assert.Equal(bd["ThingBlue"], "ThingBlue");
509509
}
510510

511+
[Fact]
512+
public void Can_Deserialize_To_Dictionary_String_String_With_Dynamic_Values ()
513+
{
514+
var doc = CreateDynamicJsonStringDictionary ();
515+
var d = new JsonDeserializer ();
516+
var response = new RestResponse { Content = doc };
517+
var bd = d.Deserialize<Dictionary<string, string>> (response);
518+
519+
Assert.Equal ("[\"Value1\",\"Value2\"]", bd["Thing1"]);
520+
Assert.Equal ("Thing2", bd["Thing2"]);
521+
Assert.Equal ("{\"Name\":\"ThingRed\",\"Color\":\"Red\"}", bd["ThingRed"]);
522+
Assert.Equal ("{\"Name\":\"ThingBlue\",\"Color\":\"Blue\"}", bd["ThingBlue"]);
523+
}
524+
511525
private string CreateJsonWithUnderscores()
512526
{
513527
var doc = new JObject();
@@ -678,5 +692,15 @@ public string CreateJsonStringDictionary()
678692
doc["ThingBlue"] = "ThingBlue";
679693
return doc.ToString();
680694
}
695+
696+
public string CreateDynamicJsonStringDictionary ()
697+
{
698+
var doc = new JObject ();
699+
doc["Thing1"] = new JArray () { "Value1", "Value2" };
700+
doc["Thing2"] = "Thing2";
701+
doc["ThingRed"] = new JObject (new JProperty ("Name", "ThingRed"), new JProperty ("Color", "Red"));
702+
doc["ThingBlue"] = new JObject (new JProperty("Name", "ThingBlue"), new JProperty ("Color", "Blue"));
703+
return doc.ToString ();
704+
}
681705
}
682706
}

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private object CreateAndMap(Type type, object element)
249249
}
250250
else if (type == typeof(string))
251251
{
252-
instance = (string)element;
252+
instance = element.ToString();
253253
}
254254
else
255255
{

0 commit comments

Comments
 (0)