Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 154cafd

Browse files
committed
Fix JsonObject null response
1 parent 47393cd commit 154cafd

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/ServiceStack.Text/Common/DeserializeDictionary.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public static ParseStringSegmentDelegate GetParseStringSegmentMethod(Type type)
8585

8686
public static JsonObject ParseJsonObject(StringSegment value)
8787
{
88+
if (value.Length == 0)
89+
return null;
90+
8891
var index = VerifyAndGetStartIndex(value, typeof(JsonObject));
8992

9093
var result = new JsonObject();
@@ -270,7 +273,7 @@ public static IDictionary<TKey, TValue> ParseDictionary<TKey, TValue>(
270273
private static int VerifyAndGetStartIndex(StringSegment value, Type createMapType)
271274
{
272275
var index = 0;
273-
if (!Serializer.EatMapStartChar(value, ref index))
276+
if (value.Length > 0 && !Serializer.EatMapStartChar(value, ref index))
274277
{
275278
//Don't throw ex because some KeyValueDataContractDeserializer don't have '{}'
276279
Tracer.Instance.WriteDebug("WARN: Map definitions should start with a '{0}', expecting serialized type '{1}', got string starting with: {2}",

tests/ServiceStack.Text.Tests/JsonTests/JsonObjectTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ public void Can_parse_empty_object_with_mixed_whitespaces()
2626
Assert.That(JsonObject.Parse("{ \n\t \n\r}"), Is.Empty);
2727
}
2828

29+
30+
public class JsonObjectResponse
31+
{
32+
public JsonObject result { get; set; }
33+
}
34+
35+
[Test]
36+
public void Can_serialize_null_JsonObject_response()
37+
{
38+
JsConfig.ThrowOnDeserializationError = true;
39+
var json = "{\"result\":null}";
40+
var dto = json.FromJson<JsonObjectResponse>();
41+
Assert.That(dto.result, Is.Null);
42+
JsConfig.ThrowOnDeserializationError = false;
43+
}
44+
2945
[Test]
3046
public void Can_Serialize_numbers()
3147
{

0 commit comments

Comments
 (0)