From 23dd1ad2e870d8816142461b46b53d3c8d5ef326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Mon, 3 Jul 2023 01:45:50 +0200 Subject: [PATCH] Custom configuration handle JSON. --- .../Hast.Common/Extensions/DictionaryExtensions.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Hastlayer/Hast.Common/Extensions/DictionaryExtensions.cs b/src/Hastlayer/Hast.Common/Extensions/DictionaryExtensions.cs index 3eedec12..108154e8 100644 --- a/src/Hastlayer/Hast.Common/Extensions/DictionaryExtensions.cs +++ b/src/Hastlayer/Hast.Common/Extensions/DictionaryExtensions.cs @@ -1,3 +1,5 @@ +using Newtonsoft.Json.Linq; + namespace System.Collections.Generic; public static class DictionaryExtensions @@ -5,7 +7,11 @@ public static class DictionaryExtensions public static T GetOrAddCustomConfiguration(this IDictionary customConfiguration, string key) where T : new() { - if (customConfiguration.TryGetValue(key, out var config)) return (T)config; + if (customConfiguration.TryGetValue(key, out var config)) + { + if (config is T typedConfig) return typedConfig; + if (config is JToken jToken) return jToken.ToObject(); + } var newInstance = new T(); customConfiguration[key] = newInstance;