Skip to content

Commit 485144d

Browse files
committed
Merge branch 'master' of https://github.com/Bumler/RestSharp into Bumler-master
2 parents a9359d6 + 745bcf7 commit 485144d

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,17 @@ public void Serialize_Json_Returns_Same_Json()
881881
Assert.AreEqual(preformattedString, result);
882882
}
883883

884+
[Test]
885+
public void Serialize_Json_Returns_Same_Json_Array()
886+
{
887+
string preformattedString = "[{ \"name\" : \"value\" }]";
888+
889+
var json = new JsonSerializer();
890+
string result = json.Serialize( preformattedString );
891+
892+
Assert.AreEqual( preformattedString, result );
893+
}
894+
884895
[Test]
885896
public void Serialize_Json_Does_Not_Double_Escape()
886897
{

RestSharp/Serialization/Json/JsonSerializer.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,39 @@ public class JsonSerializer : IRestSerializer, IWithRootElement
1515
{
1616
/// <summary>
1717
/// Serialize the object as JSON
18+
/// If the object is already a serialized string returns it's value
1819
/// </summary>
1920
/// <param name="obj">Object to serialize</param>
2021
/// <returns>JSON as String</returns>
2122
public string Serialize(object obj)
2223
{
23-
if (obj is string value)
24+
if( IsSerializedString( obj, out var serializedString ) )
2425
{
25-
var trimmed = value.Trim();
26-
if (trimmed.StartsWith("{") && trimmed.EndsWith("}"))
26+
return serializedString;
27+
}
28+
29+
return SimpleJson.SimpleJson.SerializeObject(obj);
30+
}
31+
32+
/// <summary>
33+
/// Determines if the object is already a serialized string.
34+
/// </summary>
35+
private static bool IsSerializedString( object obj, out string serializedString )
36+
{
37+
if( obj is string value )
38+
{
39+
string trimmed = value.Trim();
40+
41+
if( ( trimmed.StartsWith( "{" ) && trimmed.EndsWith( "}" ) )
42+
|| ( trimmed.StartsWith( "[{" ) && trimmed.EndsWith( "}]" ) ) )
2743
{
28-
return value;
44+
serializedString = value;
45+
return true;
2946
}
3047
}
3148

32-
return SimpleJson.SimpleJson.SerializeObject(obj);
49+
serializedString = null;
50+
return false;
3351
}
3452

3553
/// <summary>

0 commit comments

Comments
 (0)