GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(infra): allow boolean strings (eg. "true") when deserializing JSON * Fixes regression after switch to `System.Text.Json`
Lars Berger12ca803d3050
diff --git a/GlazeWM.Infrastructure/Serialization/JsonService.cs b/GlazeWM.Infrastructure/Serialization/JsonService.csindex 903571fd..b576f5bc 100644--- a/GlazeWM.Infrastructure/Serialization/JsonService.cs+++ b/GlazeWM.Infrastructure/Serialization/JsonService.cs@@ -15,7 +15,9 @@ namespace GlazeWM.Infrastructure.SerializationIncludeFields = true,Converters = {// Enable strings to be mapped to a C# enum (eg. `BarPosition` enum).- new JsonStringEnumConverter()+ new JsonStringEnumConverter(),+ // Enable boolean strings to be mapped to a C# bool (eg. `"true"` -> `true`).+ new JsonStringBoolConverter(),}};diff --git a/GlazeWM.Infrastructure/Serialization/JsonStringBoolConverter.cs b/GlazeWM.Infrastructure/Serialization/JsonStringBoolConverter.csnew file mode 100644index 00000000..4ad58264--- /dev/null+++ b/GlazeWM.Infrastructure/Serialization/JsonStringBoolConverter.cs@@ -0,0 +1,33 @@+using System;+using System.Text.Json;+using System.Text.Json.Serialization;++namespace GlazeWM.Infrastructure.Serialization+{+ public class JsonStringBoolConverter : JsonConverter<bool>+ {+ /// <summary>+ /// Allow boolean strings when deserializing to a bool (eg. "true" -> true).+ /// </summary>+ public override bool Read(+ ref Utf8JsonReader reader,+ Type typeToConvert,+ JsonSerializerOptions options)+ {+ return reader.TokenType switch+ {+ JsonTokenType.True => true,+ JsonTokenType.False => false,+ JsonTokenType.String => bool.TryParse(reader.GetString(), out var parsedBool)+ ? parsedBool+ : throw new JsonException(),+ _ => throw new JsonException(),+ };+ }++ public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)+ {+ writer.WriteBooleanValue(value);+ }+ }+}diff --git a/GlazeWM.Infrastructure/Serialization/YamlService.cs b/GlazeWM.Infrastructure/Serialization/YamlService.csindex 38908811..bc9c425e 100644--- a/GlazeWM.Infrastructure/Serialization/YamlService.cs+++ b/GlazeWM.Infrastructure/Serialization/YamlService.cs@@ -25,7 +25,7 @@ namespace GlazeWM.Infrastructure.Serializationvar yamlObject = _yamlDeserializer.Deserialize(new StringReader(input));// Convert key-value pairs into a JSON string.- var jsonString = _jsonService.Serialize(yamlObject, converters);+ var jsonString = _jsonService.Serialize(yamlObject, new List<JsonConverter>());return _jsonService.Deserialize<T>(jsonString, converters);}