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

Commit 7152992

Browse files
committed
unencode object dictionary values
1 parent 69085b6 commit 7152992

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/ServiceStack.Text/Common/DeserializeDictionary.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public static IDictionary<TKey, TValue> ParseDictionary<TKey, TValue>(
276276
{
277277
to[mapKey] = (TValue)(tryToParseItemsAsPrimitiveTypes && elementStartIndex < valueLength
278278
? DeserializeType<TSerializer>.ParsePrimitive(elementValue.Value(), value[elementStartIndex])
279-
: parseValueFn(elementValue));
279+
: parseValueFn(elementValue).Value());
280280
}
281281
}
282282
else
@@ -288,7 +288,7 @@ public static IDictionary<TKey, TValue> ParseDictionary<TKey, TValue>(
288288
}
289289
else
290290
{
291-
to[mapKey] = (TValue)parseValueFn(elementValue);
291+
to[mapKey] = (TValue)parseValueFn(elementValue).Value();
292292
}
293293
}
294294

src/ServiceStack.Text/StringSpanExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public static string Value(this ReadOnlySpan<char> value) => value.IsEmpty
2424
? ""
2525
: value.ToString();
2626

27+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
28+
internal static object Value(this object obj) =>
29+
obj is string value && value.Length == 1 && value[0] == TypeConstants.NonWidthWhiteSpace
30+
? ""
31+
: obj;
32+
2733
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2834
public static bool IsNullOrEmpty(this ReadOnlySpan<char> value) => value.IsEmpty || (value.Length == 1 && value[0] == TypeConstants.NonWidthWhiteSpace);
2935

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System.Collections.Generic;
2+
using System.IO;
23
using Northwind.Common.DataModel;
34
using NUnit.Framework;
45
using ServiceStack.Text.Json;
@@ -45,5 +46,22 @@ public void Does_serialize_property_Empty_String()
4546
Assert.That(fromMs.Name, Is.EqualTo(dto.Name));
4647
JS.UnConfigure();
4748
}
49+
50+
[Test]
51+
public void Does_serialize_object_dictionary()
52+
{
53+
JsConfig.ConvertObjectTypesIntoStringDictionary = true;
54+
55+
var value = "{Number:10330,CountryKey:DE,FederalStateKey:\"\",Salutation:,City:''}";
56+
var map = (Dictionary<string,object>) value.FromJsv<object>();
57+
58+
Assert.That(map["Number"], Is.EqualTo("10330"));
59+
Assert.That(map["CountryKey"], Is.EqualTo("DE"));
60+
Assert.That(map["FederalStateKey"], Is.EqualTo(""));
61+
Assert.That(map["Salutation"], Is.EqualTo(""));
62+
Assert.That(map["City"], Is.EqualTo("''"));
63+
64+
JsConfig.ConvertObjectTypesIntoStringDictionary = false;
65+
}
4866
}
4967
}

0 commit comments

Comments
 (0)