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

Commit d28ba58

Browse files
committed
workaround dirty tests by only checking objSerializer in cached generic parse fn
1 parent 3d79b22 commit d28ba58

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

src/ServiceStack.Text/Common/DeserializeDictionary.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ public static ParseStringSpanDelegate GetParseStringSpanMethod(Type type)
4545
return s => ParseIDictionary(s, type);
4646
}
4747

48-
throw new ArgumentException(string.Format("Type {0} is not of type IDictionary<,>", type.FullName));
48+
throw new ArgumentException($"Type {type.FullName} is not of type IDictionary<,>");
4949
}
5050

5151
//optimized access for regularly used types
5252
if (type == typeof(Dictionary<string, string>))
5353
return ParseStringDictionary;
54-
if (type == typeof(Dictionary<string, object>) && Json.JsonTypeSerializer.Instance.ObjectDeserializer != null)
55-
return s => Json.JsonTypeSerializer.Instance.ObjectDeserializer(s);
5654
if (type == typeof(JsonObject))
5755
return ParseJsonObject;
5856
if (typeof(JsonObject).IsAssignableFrom(type))
@@ -229,6 +227,14 @@ public static IDictionary<TKey, TValue> ParseDictionary<TKey, TValue>(
229227
{
230228
if (value.IsEmpty) return null;
231229

230+
var to = (createMapType == null)
231+
? new Dictionary<TKey, TValue>()
232+
: (IDictionary<TKey, TValue>)createMapType.CreateInstance();
233+
234+
var objDeserializer = Json.JsonTypeSerializer.Instance.ObjectDeserializer;
235+
if (to is Dictionary<string, object> && objDeserializer != null)
236+
return (IDictionary<TKey,TValue>) objDeserializer(value);
237+
232238
var config = JsConfig.GetConfig();
233239

234240
var tryToParseItemsAsDictionaries =
@@ -238,10 +244,6 @@ public static IDictionary<TKey, TValue> ParseDictionary<TKey, TValue>(
238244

239245
var index = VerifyAndGetStartIndex(value, createMapType);
240246

241-
var to = (createMapType == null)
242-
? new Dictionary<TKey, TValue>()
243-
: (IDictionary<TKey, TValue>)createMapType.CreateInstance();
244-
245247
if (Json.JsonTypeSerializer.IsEmptyMap(value, index)) return to;
246248

247249
var valueLength = value.Length;

src/ServiceStack.Text/Common/DeserializeListWithElements.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,18 @@ public static ICollection<T> ParseGenericList(string value, Type createListType,
161161

162162
public static ICollection<T> ParseGenericList(ReadOnlySpan<char> value, Type createListType, ParseStringSpanDelegate parseFn)
163163
{
164-
if ((value = DeserializeListWithElements<TSerializer>.StripList(value)).IsEmpty)
165-
return null;
166-
167-
var isReadOnly = createListType != null
168-
&& (createListType.IsGenericType && createListType.GetGenericTypeDefinition() == typeof(ReadOnlyCollection<>));
169-
164+
var isReadOnly = createListType != null && (createListType.IsGenericType && createListType.GetGenericTypeDefinition() == typeof(ReadOnlyCollection<>));
170165
var to = (createListType == null || isReadOnly)
171166
? new List<T>()
172167
: (ICollection<T>)createListType.CreateInstance();
173168

169+
var objSerializer = Json.JsonTypeSerializer.Instance.ObjectDeserializer;
170+
if (to is List<object> && objSerializer != null)
171+
return (ICollection<T>)objSerializer(value);
172+
173+
if ((value = DeserializeListWithElements<TSerializer>.StripList(value)).IsEmpty)
174+
return null;
175+
174176
if (value.IsNullOrEmpty())
175177
return isReadOnly ? (ICollection<T>)Activator.CreateInstance(createListType, to) : to;
176178

@@ -270,9 +272,6 @@ public static ParseStringSpanDelegate GetParseStringSpanFn()
270272
if (typeof(T) == typeof(List<int>))
271273
return DeserializeListWithElements<TSerializer>.ParseIntList;
272274

273-
if (typeof(T) == typeof(List<object>) && Json.JsonTypeSerializer.Instance.ObjectDeserializer != null)
274-
return s => Json.JsonTypeSerializer.Instance.ObjectDeserializer(s);
275-
276275
var elementType = listInterface.GetGenericArguments()[0];
277276

278277
var supportedTypeParseMethod = DeserializeListWithElements<TSerializer>.Serializer.GetParseStringSpanFn(elementType);

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@ public void CanDeserializeDictionaryOfComplexTypes()
1212
{
1313
JsConfig.ConvertObjectTypesIntoStringDictionary = true;
1414

15-
var dict = new Dictionary<string, object>();
16-
17-
dict["ChildDict"] = new Dictionary<string, object>
18-
{
19-
{"age", 12},
20-
{"name", "mike"}
15+
var dict = new Dictionary<string, object> {
16+
["ChildDict"] = new Dictionary<string, object> {{"age", 12}, {"name", "mike"}},
17+
["ChildIntList"] = new List<int> {1, 2, 3},
18+
["ChildStringList"] = new List<string> {"a", "b", "c"},
19+
["ChildObjectList"] = new List<object> {1, "cat", new Dictionary<string, object> {{"s", "s"}, {"n", 1}}}
2120
};
2221

23-
dict["ChildIntList"] = new List<int> {1, 2, 3};
24-
dict["ChildStringList"] = new List<string> {"a", "b", "c"};
25-
dict["ChildObjectList"] = new List<object> {1, "cat", new Dictionary<string, object> {{"s", "s"}, {"n", 1}}};
26-
2722
var serialized = JsonSerializer.SerializeToString(dict);
2823

2924
var deserialized = JsonSerializer.DeserializeFromString<Dictionary<string, object>>(serialized);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public void Can_deserialize_custom_JsonObject_with_incorrect_payload()
505505
}
506506
catch (FormatException) {}
507507
}
508-
508+
509509
class HasObjectDictionary
510510
{
511511
public Dictionary<string, object> Properties { get; set; }
@@ -544,6 +544,5 @@ public void Can_deserialize_unknown_ObjectList()
544544

545545
JS.UnConfigure();
546546
}
547-
548547
}
549548
}

0 commit comments

Comments
 (0)