Skip to content

Commit

Permalink
Custom configuration handle JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Jul 2, 2023
1 parent 4b5e0ec commit 23dd1ad
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Hastlayer/Hast.Common/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using Newtonsoft.Json.Linq;

namespace System.Collections.Generic;

public static class DictionaryExtensions
{
public static T GetOrAddCustomConfiguration<T>(this IDictionary<string, object> 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<T>();
}

var newInstance = new T();
customConfiguration[key] = newInstance;
Expand Down

1 comment on commit 23dd1ad

@sarahelsaig
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary, otherwise the Hast.Samples.Consumer GUI breaks like below:
image
This is needed, since the GUI deserializes stored configuration (here)

Please sign in to comment.