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

Commit b7f2b36

Browse files
committed
Resolve issue with empty string when using custom DeSerializeFn
1 parent ea31fcf commit b7f2b36

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/ServiceStack.Text/Json/JsonTypeSerializer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,9 @@ internal static ReadOnlySpan<char> ParseJsonString(ReadOnlySpan<char> json, ref
421421

422422
private static string UnescapeJsonString(string json, ref int index)
423423
{
424-
return UnescapeJsonString(json.AsSpan(), ref index).ToString();
424+
return json != null
425+
? UnescapeJsonString(json.AsSpan(), ref index).ToString()
426+
: null;
425427
}
426428

427429
private static ReadOnlySpan<char> UnescapeJsonString(ReadOnlySpan<char> json, ref int index) =>

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,5 +655,21 @@ public void Can_deserialize_ThrowValidation_with_null_values()
655655
Assert.That(dto.Email, Is.EqualTo("invalidemail"));
656656
}
657657

658+
class TestTrim
659+
{
660+
public string Description { get; set; }
661+
}
662+
663+
[Test]
664+
public void Can_deserialize_custom_string_deserializer()
665+
{
666+
JsConfig<string>.DeSerializeFn = str => str.Trim();
667+
var json = "{\"description\":null}";
668+
var dto = json.FromJson<TestTrim>();
669+
Assert.That(dto.Description, Is.Null);
670+
JsConfig<string>.DeSerializeFn = null;
671+
}
672+
673+
658674
}
659675
}

0 commit comments

Comments
 (0)