Skip to content

Commit

Permalink
Merge pull request #435 from bid-soft/dev
Browse files Browse the repository at this point in the history
SimpleJson: fixed de/serialization for dictionaries
  • Loading branch information
MSchmoecker authored Jul 31, 2024
2 parents 91c2183 + c62e1af commit adc5b07
Showing 1 changed file with 18 additions and 2 deletions.
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

0 comments on commit adc5b07

Please sign in to comment.