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

Commit

Permalink
Fix JsonObject null response
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Dec 10, 2017
1 parent 47393cd commit 154cafd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ServiceStack.Text/Common/DeserializeDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -270,7 +273,7 @@ public static IDictionary<TKey, TValue> ParseDictionary<TKey, TValue>(
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}",
Expand Down
16 changes: 16 additions & 0 deletions tests/ServiceStack.Text.Tests/JsonTests/JsonObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonObjectResponse>();
Assert.That(dto.result, Is.Null);
JsConfig.ThrowOnDeserializationError = false;
}

[Test]
public void Can_Serialize_numbers()
{
Expand Down

0 comments on commit 154cafd

Please sign in to comment.