From 154cafde88a89c152b7c131b398b4d8bf95e60e8 Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Sun, 10 Dec 2017 14:51:28 -0500 Subject: [PATCH] Fix JsonObject null response --- .../Common/DeserializeDictionary.cs | 5 ++++- .../JsonTests/JsonObjectTests.cs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ServiceStack.Text/Common/DeserializeDictionary.cs b/src/ServiceStack.Text/Common/DeserializeDictionary.cs index f72c56476..8e8bad61c 100644 --- a/src/ServiceStack.Text/Common/DeserializeDictionary.cs +++ b/src/ServiceStack.Text/Common/DeserializeDictionary.cs @@ -85,6 +85,9 @@ public static ParseStringSegmentDelegate GetParseStringSegmentMethod(Type type) public static JsonObject ParseJsonObject(StringSegment value) { + if (value.Length == 0) + return null; + var index = VerifyAndGetStartIndex(value, typeof(JsonObject)); var result = new JsonObject(); @@ -270,7 +273,7 @@ public static IDictionary ParseDictionary( private static int VerifyAndGetStartIndex(StringSegment value, Type createMapType) { var index = 0; - if (!Serializer.EatMapStartChar(value, ref index)) + if (value.Length > 0 && !Serializer.EatMapStartChar(value, ref index)) { //Don't throw ex because some KeyValueDataContractDeserializer don't have '{}' Tracer.Instance.WriteDebug("WARN: Map definitions should start with a '{0}', expecting serialized type '{1}', got string starting with: {2}", diff --git a/tests/ServiceStack.Text.Tests/JsonTests/JsonObjectTests.cs b/tests/ServiceStack.Text.Tests/JsonTests/JsonObjectTests.cs index 0d64ba7dd..3434966ef 100644 --- a/tests/ServiceStack.Text.Tests/JsonTests/JsonObjectTests.cs +++ b/tests/ServiceStack.Text.Tests/JsonTests/JsonObjectTests.cs @@ -26,6 +26,22 @@ public void Can_parse_empty_object_with_mixed_whitespaces() Assert.That(JsonObject.Parse("{ \n\t \n\r}"), Is.Empty); } + + public class JsonObjectResponse + { + public JsonObject result { get; set; } + } + + [Test] + public void Can_serialize_null_JsonObject_response() + { + JsConfig.ThrowOnDeserializationError = true; + var json = "{\"result\":null}"; + var dto = json.FromJson(); + Assert.That(dto.result, Is.Null); + JsConfig.ThrowOnDeserializationError = false; + } + [Test] public void Can_Serialize_numbers() {