Skip to content

Commit

Permalink
more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tvatavuk committed Sep 28, 2023
1 parent ac79071 commit 70f05db
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
};
}
Expand Down
42 changes: 35 additions & 7 deletions ToSic.Cre8Magic.Client/Settings/Json/JsonConverterBase.cs
Original file line number Diff line number Diff line change
@@ -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<T>: JsonConverter<T>
public abstract class JsonConverterBase<T> : JsonConverter<T>
{
public ILogger Logger { get; }

protected JsonConverterBase(ILogger logger) => Logger = logger;

private static readonly ConditionalWeakTable<JsonConverterBase<T>, 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<T>(GetOptionsWithoutThisConverter(options));
Logger.LogInformation("2sic# Deserializing {Type} from {Json}", typeof(T), jsonObject);

IsInsideConverter = true;
var result = jsonObject.Deserialize<T>(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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion ToSic.Cre8Magic.Client/Settings/PairOnOffJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
}
Expand Down

0 comments on commit 70f05db

Please sign in to comment.