Skip to content

Commit 06f1d50

Browse files
committed
Merge pull request restsharp#410 from restsharp/object-with-primitive-value-fix
Object with primitive value fix
2 parents 6230c18 + a19f681 commit 06f1d50

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void Can_Deserialize_List_of_Guid()
130130
{
131131
Guid ID1 = new Guid("b0e5c11f-e944-478c-aadd-753b956d0c8c");
132132
Guid ID2 = new Guid("809399fa-21c4-4dca-8dcd-34cb697fbca0");
133-
var data = new JsonObject();
133+
var data = new JsonObject();
134134
data["Ids"] = new JsonArray() {ID1, ID2};
135135

136136
var d = new JsonDeserializer();
@@ -632,6 +632,8 @@ public void Can_Deserialize_To_Dictionary_String_String_With_Dynamic_Values ()
632632
Assert.Equal ("{\"Name\":\"ThingBlue\",\"Color\":\"Blue\"}", bd["ThingBlue"]);
633633
}
634634

635+
<<<<<<< HEAD
636+
<<<<<<< HEAD
635637
[Fact]
636638
public void Can_Deserialize_Decimal_With_Four_Zeros_After_Floating_Point()
637639
{
@@ -643,6 +645,14 @@ public void Can_Deserialize_Decimal_With_Four_Zeros_After_Floating_Point()
643645
Assert.Equal(result.Value, .00005557m);
644646
}
645647

648+
[Fact]
649+
public void Can_Deserialize_Object_Type_Property_With_Primitive_Vale()
650+
{
651+
var payload = GetPayLoad<ObjectProperties>("objectproperty.txt");
652+
653+
Assert.Equal(42L, payload.ObjectProperty);
654+
}
655+
646656
private string CreateJsonWithUnderscores()
647657
{
648658
var doc = new JsonObject();
@@ -657,11 +667,11 @@ private string CreateJsonWithUnderscores()
657667
doc["url"] = "http://example.com";
658668
doc["url_path"] = "/foo/bar";
659669

660-
670+
661671
doc["best_friend"] = new JsonObject {
662-
{"name", "The Fonz"},
672+
{"name", "The Fonz"},
663673
{"since", 1952}
664-
};
674+
};
665675

666676
var friendsArray = new JsonArray();
667677
for (int i = 0; i < 10; i++)
@@ -677,7 +687,7 @@ private string CreateJsonWithUnderscores()
677687
var foesArray = new JsonObject{
678688
{"dict1", new JsonObject{{"nickname", "Foe 1"}}},
679689
{"dict2", new JsonObject{{"nickname", "Foe 2"}}}
680-
};
690+
};
681691

682692
doc["foes"] = foesArray;
683693

RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<Content Include="SampleData\iso8601datetimes.txt">
118118
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
119119
</Content>
120+
<Content Include="SampleData\objectproperty.txt">
121+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
122+
</Content>
120123
<Content Include="SampleData\underscore_prefix.txt">
121124
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
122125
</Content>

RestSharp.Tests/SampleClasses/misc.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public class OrderedProperties
129129
public DateTime StartDate { get; set; }
130130
}
131131

132+
public class ObjectProperties
133+
{
134+
public object ObjectProperty { get; set; }
135+
}
136+
132137
public class DatabaseCollection : List<Database>
133138
{
134139
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ObjectProperty": 42
3+
}

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ private object ConvertValue(Type type, object value)
165165
type = type.GetGenericArguments()[0];
166166
}
167167

168+
if (type == typeof(System.Object) && value != null)
169+
{
170+
type = value.GetType();
171+
}
172+
168173
if (type.IsPrimitive)
169174
{
170175
// no primitives can contain quotes so we can safely remove them

0 commit comments

Comments
 (0)