diff --git a/ToSic.Cre8Magic.Client/Settings/DesignSettingsJsonConverter.cs b/ToSic.Cre8Magic.Client/Settings/DesignSettingsJsonConverter.cs index 17cd354..1c19b62 100644 --- a/ToSic.Cre8Magic.Client/Settings/DesignSettingsJsonConverter.cs +++ b/ToSic.Cre8Magic.Client/Settings/DesignSettingsJsonConverter.cs @@ -30,7 +30,7 @@ public override void Write(Utf8JsonWriter writer, T? pair, JsonSerializerOptions public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - Logger.LogTrace($"Reading {typeof(T)} / {typeToConvert}."); + Logger.LogInformation($"2sic# Reading DesignSettingsJsonConverter {typeof(T)} / {typeToConvert}."); var jsonNode = JsonNode.Parse(ref reader); const string errArray = "Error unexpected data - array instead of string or object"; @@ -39,7 +39,7 @@ public override void Write(Utf8JsonWriter writer, T? pair, JsonSerializerOptions null => null, JsonArray _ => ConvertValue(errArray), JsonValue jValue => ConvertValue(jValue.ToString()), - JsonObject jObject => ConvertObject(jObject, options), + JsonObject jObject => ConvertObject(jObject, GetOptionsWithoutThisConverter(options)), _ => null, }; } diff --git a/ToSic.Cre8Magic.Client/Settings/Json/JsonConverterBase.cs b/ToSic.Cre8Magic.Client/Settings/Json/JsonConverterBase.cs index e1f2cdc..6b5ce99 100644 --- a/ToSic.Cre8Magic.Client/Settings/Json/JsonConverterBase.cs +++ b/ToSic.Cre8Magic.Client/Settings/Json/JsonConverterBase.cs @@ -1,34 +1,62 @@ -using System.Text.Json; +using System.Runtime.CompilerServices; +using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; using Microsoft.Extensions.Logging; namespace ToSic.Cre8Magic.Client.Settings.Json; -public abstract class JsonConverterBase: JsonConverter +public abstract class JsonConverterBase : JsonConverter { public ILogger Logger { get; } protected JsonConverterBase(ILogger logger) => Logger = logger; + private static readonly ConditionalWeakTable, BoxedBool> ConverterFlags = new(); + + private bool IsInsideConverter + { + get => ConverterFlags.GetValue(this, _ => new BoxedBool()).Value; + set => ConverterFlags.GetOrCreateValue(this).Value = value; + } + + private class BoxedBool + { + public bool Value; + } protected JsonSerializerOptions GetOptionsWithoutThisConverter(JsonSerializerOptions options) { + if (IsInsideConverter) + { + Logger.LogInformation("2sic# Already inside converter {Converter}", this); + return options; // Return the original options if we're already inside the converter + } + JsonSerializerOptions optionsWithoutConverter = new(options); - optionsWithoutConverter.Converters.Remove(this); + if (!optionsWithoutConverter.Converters.Remove(this)) + Logger.LogWarning("2sic# Could not remove converter {Converter} from options", this); + else + Logger.LogInformation("2sic# Removed converter {Converter} from options", this); return optionsWithoutConverter; } - + protected T? ConvertObject(JsonObject jsonObject, JsonSerializerOptions options) { try { - Logger.LogTrace("Deserializing {Type} from {Json}", typeof(T), jsonObject); - return jsonObject.Deserialize(GetOptionsWithoutThisConverter(options)); + Logger.LogInformation("2sic# Deserializing {Type} from {Json}", typeof(T), jsonObject); + + IsInsideConverter = true; + var result = jsonObject.Deserialize(GetOptionsWithoutThisConverter(options)); + IsInsideConverter = false; + + return result; } catch { - Logger.LogError("Error while deserializing {Type} from {Json}", typeof(T), jsonObject); + Logger.LogError("2sic# Error while deserializing {Type} from {Json}", typeof(T), jsonObject); + IsInsideConverter = false; // Ensure the flag is reset even if an exception occurs throw; } } diff --git a/ToSic.Cre8Magic.Client/Settings/PairOnOffJsonConverter.cs b/ToSic.Cre8Magic.Client/Settings/PairOnOffJsonConverter.cs index ab54af5..46f31b9 100644 --- a/ToSic.Cre8Magic.Client/Settings/PairOnOffJsonConverter.cs +++ b/ToSic.Cre8Magic.Client/Settings/PairOnOffJsonConverter.cs @@ -40,13 +40,14 @@ public override void Write(Utf8JsonWriter writer, PairOnOff? pair, JsonSerialize public override PairOnOff? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + Logger.LogInformation($"2sic# Reading PairOnOffJsonConverter / {typeToConvert}."); var x = JsonNode.Parse(ref reader); return x switch { null => null, JsonArray jArray => ConvertArray(jArray), JsonValue jValue => new() { On = jValue.ToString() }, - JsonObject jObject => ConvertObject(jObject, options), + JsonObject jObject => ConvertObject(jObject, GetOptionsWithoutThisConverter(options)), _ => new() { On = "error", Off = "error" }, }; } diff --git a/ToSic.Cre8Magic.Client/Themes/Settings/ThemePartJsonConverter.cs b/ToSic.Cre8Magic.Client/Themes/Settings/ThemePartJsonConverter.cs index 0a4dfbd..2eed265 100644 --- a/ToSic.Cre8Magic.Client/Themes/Settings/ThemePartJsonConverter.cs +++ b/ToSic.Cre8Magic.Client/Themes/Settings/ThemePartJsonConverter.cs @@ -39,13 +39,14 @@ public override void Write(Utf8JsonWriter writer, MagicThemePartSettings? part, public override MagicThemePartSettings? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + Logger.LogInformation($"2sic# Reading ThemePartJsonConverter / {typeToConvert}."); var x = JsonNode.Parse(ref reader); return x switch { null => null, JsonArray jArray => Dummy(), JsonValue jValue => ConvertValue(jValue), - JsonObject jObject => ConvertObject(jObject, options), + JsonObject jObject => ConvertObject(jObject, GetOptionsWithoutThisConverter(options)), _ => Dummy(), }; }