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

Commit f7337e6

Browse files
committed
Resolve object empty string issue when using JS.Configure()
1 parent 5d98d20 commit f7337e6

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/ServiceStack.Text/Common/DeserializeType.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ public static object ObjectStringToType(ReadOnlySpan<char> strType)
9494
return primitiveType;
9595

9696
if (Serializer.ObjectDeserializer != null && typeof(TSerializer) == typeof(Json.JsonTypeSerializer))
97-
return Serializer.ObjectDeserializer(strType);
97+
return !strType.IsNullOrEmpty()
98+
? Serializer.ObjectDeserializer(strType)
99+
: strType.Value();
98100

99101
return Serializer.UnescapeString(strType).Value();
100102
}

src/ServiceStack.Text/Json/JsonUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static class JsonUtils
4242
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4343
public static bool IsWhiteSpace(char c)
4444
{
45-
return c == ' ' || (c >= '\x0009' && c <= '\x000d') || c == '\x00a0' || c == '\x0085';
45+
return c == ' ' || (c >= '\x0009' && c <= '\x000d') || c == '\x00a0' || c == '\x0085' || c == TypeConstants.NonWidthWhiteSpace;
4646
}
4747

4848
public static void WriteString(TextWriter writer, string value)

tests/ServiceStack.Text.Tests/Issues/WhitespaceIssues.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using NUnit.Framework;
1+
using System.IO;
2+
using Northwind.Common.DataModel;
3+
using NUnit.Framework;
4+
using ServiceStack.Text.Json;
25

36
namespace ServiceStack.Text.Tests.Issues
47
{
@@ -20,5 +23,27 @@ public void Does_deserialize_empty_string_to_object()
2023
var obj = json.FromJson<object>();
2124
Assert.That(obj, Is.EqualTo(""));
2225
}
26+
27+
public class ObjectEmptyStringTest
28+
{
29+
public object Name { get; set; }
30+
}
31+
32+
[Test]
33+
public void Does_serialize_property_Empty_String()
34+
{
35+
JS.Configure();
36+
var dto = new ObjectEmptyStringTest { Name = "" };
37+
var json = dto.ToJson();
38+
39+
var fromJson = json.FromJson<ObjectEmptyStringTest>();
40+
Assert.That(fromJson.Name, Is.EqualTo(dto.Name));
41+
42+
var utf8 = json.ToUtf8Bytes();
43+
var ms = new MemoryStream(utf8);
44+
var fromMs = JsonSerializer.DeserializeFromStream<ObjectEmptyStringTest>(ms);
45+
Assert.That(fromMs.Name, Is.EqualTo(dto.Name));
46+
JS.UnConfigure();
47+
}
2348
}
2449
}

0 commit comments

Comments
 (0)