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

Commit 3d79b22

Browse files
committed
Add support for deserializing List<object> and Dictionary<string,object> with #Script
1 parent e43d901 commit 3d79b22

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

src/ServiceStack.Text/Common/DeserializeDictionary.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using System.Collections;
1515
using System.Collections.Generic;
1616
using System.Threading;
17-
using ServiceStack.Text.Json;
1817

1918
namespace ServiceStack.Text.Common
2019
{
@@ -51,13 +50,11 @@ public static ParseStringSpanDelegate GetParseStringSpanMethod(Type type)
5150

5251
//optimized access for regularly used types
5352
if (type == typeof(Dictionary<string, string>))
54-
{
5553
return ParseStringDictionary;
56-
}
54+
if (type == typeof(Dictionary<string, object>) && Json.JsonTypeSerializer.Instance.ObjectDeserializer != null)
55+
return s => Json.JsonTypeSerializer.Instance.ObjectDeserializer(s);
5756
if (type == typeof(JsonObject))
58-
{
5957
return ParseJsonObject;
60-
}
6158
if (typeof(JsonObject).IsAssignableFrom(type))
6259
{
6360
var method = typeof(DeserializeDictionary<TSerializer>).GetMethod("ParseInheritedJsonObject");
@@ -89,7 +86,7 @@ public static ParseStringSpanDelegate GetParseStringSpanMethod(Type type)
8986

9087
var result = new T();
9188

92-
if (JsonTypeSerializer.IsEmptyMap(value, index)) return result;
89+
if (Json.JsonTypeSerializer.IsEmptyMap(value, index)) return result;
9390

9491
var valueLength = value.Length;
9592
while (index < valueLength)
@@ -119,7 +116,7 @@ public static JsonObject ParseJsonObject(ReadOnlySpan<char> value)
119116

120117
var result = new JsonObject();
121118

122-
if (JsonTypeSerializer.IsEmptyMap(value, index)) return result;
119+
if (Json.JsonTypeSerializer.IsEmptyMap(value, index)) return result;
123120

124121
var valueLength = value.Length;
125122
while (index < valueLength)
@@ -151,7 +148,7 @@ public static Dictionary<string, string> ParseStringDictionary(ReadOnlySpan<char
151148

152149
var result = new Dictionary<string, string>();
153150

154-
if (JsonTypeSerializer.IsEmptyMap(value, index)) return result;
151+
if (Json.JsonTypeSerializer.IsEmptyMap(value, index)) return result;
155152

156153
var valueLength = value.Length;
157154
while (index < valueLength)
@@ -185,7 +182,7 @@ public static IDictionary ParseIDictionary(ReadOnlySpan<char> value, Type dictTy
185182

186183
var to = (IDictionary)dictType.CreateInstance();
187184

188-
if (JsonTypeSerializer.IsEmptyMap(value, index)) return to;
185+
if (Json.JsonTypeSerializer.IsEmptyMap(value, index)) return to;
189186

190187
var valueLength = value.Length;
191188
while (index < valueLength)
@@ -245,7 +242,7 @@ public static IDictionary<TKey, TValue> ParseDictionary<TKey, TValue>(
245242
? new Dictionary<TKey, TValue>()
246243
: (IDictionary<TKey, TValue>)createMapType.CreateInstance();
247244

248-
if (JsonTypeSerializer.IsEmptyMap(value, index)) return to;
245+
if (Json.JsonTypeSerializer.IsEmptyMap(value, index)) return to;
249246

250247
var valueLength = value.Length;
251248
while (index < valueLength)
@@ -339,8 +336,9 @@ public static object ParseDictionaryType(ReadOnlySpan<char> value, Type createMa
339336
do
340337
{
341338
snapshot = ParseDelegateCache;
342-
newCache = new Dictionary<TypesKey, ParseDictionaryDelegate>(ParseDelegateCache);
343-
newCache[key] = parseDelegate;
339+
newCache = new Dictionary<TypesKey, ParseDictionaryDelegate>(ParseDelegateCache) {
340+
[key] = parseDelegate
341+
};
344342

345343
} while (!ReferenceEquals(
346344
Interlocked.CompareExchange(ref ParseDelegateCache, newCache, snapshot), snapshot));

src/ServiceStack.Text/Common/DeserializeListWithElements.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ public static ParseStringSpanDelegate GetParseStringSpanFn()
269269

270270
if (typeof(T) == typeof(List<int>))
271271
return DeserializeListWithElements<TSerializer>.ParseIntList;
272+
273+
if (typeof(T) == typeof(List<object>) && Json.JsonTypeSerializer.Instance.ObjectDeserializer != null)
274+
return s => Json.JsonTypeSerializer.Instance.ObjectDeserializer(s);
272275

273276
var elementType = listInterface.GetGenericArguments()[0];
274277

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,45 @@ public void Can_deserialize_custom_JsonObject_with_incorrect_payload()
505505
}
506506
catch (FormatException) {}
507507
}
508-
508+
509+
class HasObjectDictionary
510+
{
511+
public Dictionary<string, object> Properties { get; set; }
512+
}
513+
514+
[Test]
515+
public void Can_deserialize_unknown_ObjectDictionary()
516+
{
517+
JS.Configure();
518+
519+
Assert.That("{\"Properties\":{\"a\":1}}".FromJson<HasObjectDictionary>().Properties["a"], Is.EqualTo(1));
520+
Assert.That("{\"Properties\":{\"a\":\"1\"}}".FromJson<HasObjectDictionary>().Properties["a"], Is.EqualTo("1"));
521+
Assert.That("{\"Properties\":{\"a\":[\"1\",\"2\"]}}".FromJson<HasObjectDictionary>().Properties["a"], Is.EquivalentTo(new[]{"1","2"}));
522+
Assert.That("{\"Properties\":{\"a\":[1,2]}}".FromJson<HasObjectDictionary>().Properties["a"], Is.EquivalentTo(new[]{1,2}));
523+
524+
JS.UnConfigure();
525+
}
526+
527+
class HasObjectList
528+
{
529+
public List<object> Properties { get; set; }
530+
}
531+
532+
[Test]
533+
public void Can_deserialize_unknown_ObjectList()
534+
{
535+
JS.Configure();
536+
537+
Assert.That("{\"Properties\":[\"1\"]}".FromJson<HasObjectList>().Properties[0], Is.EqualTo("1"));
538+
Assert.That("{\"Properties\":[1]}".FromJson<HasObjectList>().Properties[0], Is.EqualTo(1));
539+
Assert.That("{\"Properties\":[[\"1\",\"2\"]]}".FromJson<HasObjectList>().Properties[0], Is.EquivalentTo(new[]{"1","2"}));
540+
Assert.That("{\"Properties\":[[1,2]]}".FromJson<HasObjectList>().Properties[0], Is.EquivalentTo(new[]{1,2}));
541+
Assert.That("{\"Properties\":[{\"a\":1}]".FromJson<HasObjectList>().Properties[0], Is.EquivalentTo(new Dictionary<string,object> {
542+
["a"] = 1
543+
}));
544+
545+
JS.UnConfigure();
546+
}
509547

510548
}
511549
}

0 commit comments

Comments
 (0)