Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleJson: fixed de/serialization for dictionaries #435

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions JotunnLib/Utils/SimpleJson.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// <copyright file="SimpleJson.cs" company="The Outercurve Foundation">
// Copyright (c) 2011, The Outercurve Foundation.
//
Expand Down Expand Up @@ -1387,6 +1387,19 @@ public virtual object DeserializeObject(object value, Type type)
? Convert.ChangeType(value, type, CultureInfo.InvariantCulture)
: value;
}
else if (value is JsonArray && ReflectionUtils.IsTypeDictionary(type))
{
var jarray = (JsonArray)value;

Type[] types = ReflectionUtils.GetGenericTypeArguments(type);
Type keyType = types[0];
Type valueType = types[1];
Type genericType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType);
IDictionary dict = (IDictionary)ConstructorCache[genericType]();

foreach (JsonObject elem in jarray) dict.Add(DeserializeObject(elem[0], keyType), DeserializeObject(elem[1], valueType));
obj = dict;
}
else
{
IDictionary<string, object> objects = value as IDictionary<string, object>;
Expand Down Expand Up @@ -1416,7 +1429,10 @@ public virtual object DeserializeObject(object value, Type type)
obj = value;
else
{
obj = ConstructorCache[type]();
var constructorDelegate = ConstructorCache[type];
if (constructorDelegate != null) obj = constructorDelegate();
else throw new MissingMethodException($"No default constructor found for {type}");

foreach (KeyValuePair<string, KeyValuePair<Type, ReflectionUtils.SetDelegate>> setter in SetCache[type])
{
object jsonValue;
Expand Down
Loading