From a9e3229fcec47df4874b4000560bb33928775b6c Mon Sep 17 00:00:00 2001 From: bid Date: Tue, 30 Jul 2024 20:05:12 +0200 Subject: [PATCH 1/2] SimpleJson: fixed de/serialization for dictionaries --- JotunnLib/Utils/SimpleJson.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/JotunnLib/Utils/SimpleJson.cs b/JotunnLib/Utils/SimpleJson.cs index 5bcea8812..aaef69ae3 100644 --- a/JotunnLib/Utils/SimpleJson.cs +++ b/JotunnLib/Utils/SimpleJson.cs @@ -1,4 +1,4 @@ -//----------------------------------------------------------------------- +//----------------------------------------------------------------------- // // Copyright (c) 2011, The Outercurve Foundation. // @@ -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 objects = value as IDictionary; From c62e1af2df3cf38ce136fd63132f742077d354b6 Mon Sep 17 00:00:00 2001 From: bid Date: Wed, 31 Jul 2024 17:23:43 +0200 Subject: [PATCH 2/2] SimpleJson: throw exception in case of default constructor not found --- JotunnLib/Utils/SimpleJson.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/JotunnLib/Utils/SimpleJson.cs b/JotunnLib/Utils/SimpleJson.cs index aaef69ae3..de8042d58 100644 --- a/JotunnLib/Utils/SimpleJson.cs +++ b/JotunnLib/Utils/SimpleJson.cs @@ -1429,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> setter in SetCache[type]) { object jsonValue;