Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

refactor: separate service for JSON serialization/deserialization (#106)

lars-berger8b9a395e02a9
diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs
index aa9b536d..dd65226c 100644
--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs
+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs
@@ -1,7 +1,7 @@
-using GlazeWM.Domain.UserConfigs.Commands;
+using GlazeWM.Domain.UserConfigs.Commands;
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Exceptions;
-using GlazeWM.Infrastructure.Yaml;
+using GlazeWM.Infrastructure.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
@@ -17,19 +17,19 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers
{
private readonly Bus _bus;
private readonly UserConfigService _userConfigService;
- private readonly YamlDeserializationService _yamlDeserializationService;
+ private readonly YamlService _yamlService;
private readonly CommandParsingService _commandParsingService;
public EvaluateUserConfigHandler(
Bus bus,
UserConfigService userConfigService,
- YamlDeserializationService yamlDeserializationService,
+ YamlService yamlService,
CommandParsingService commandParsingService
)
{
_bus = bus;
_userConfigService = userConfigService;
- _yamlDeserializationService = yamlDeserializationService;
+ _yamlService = yamlService;
_commandParsingService = commandParsingService;
}
@@ -90,9 +90,9 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers
try
{
var userConfigLines = File.ReadAllLines(userConfigPath);
- var input = new StringReader(string.Join(Environment.NewLine, userConfigLines));
+ var input = string.Join(Environment.NewLine, userConfigLines);
- return _yamlDeserializationService.Deserialize<UserConfig>(
+ return _yamlService.Deserialize<UserConfig>(
input,
new List<JsonConverter>() { new BarComponentConfigConverter() }
);
diff --git a/GlazeWM.Infrastructure/DependencyInjection.cs b/GlazeWM.Infrastructure/DependencyInjection.cs
index 331fa643..3af93651 100644
--- a/GlazeWM.Infrastructure/DependencyInjection.cs
+++ b/GlazeWM.Infrastructure/DependencyInjection.cs
@@ -2,7 +2,7 @@
using GlazeWM.Infrastructure.Bussing.CommandHandlers;
using GlazeWM.Infrastructure.Bussing.Commands;
using GlazeWM.Infrastructure.WindowsApi;
-using GlazeWM.Infrastructure.Yaml;
+using GlazeWM.Infrastructure.Serialization;
using Microsoft.Extensions.DependencyInjection;
namespace GlazeWM.Infrastructure
@@ -16,7 +16,8 @@ namespace GlazeWM.Infrastructure
services.AddSingleton<SystemEventService>();
services.AddSingleton<SystemTrayService>();
services.AddSingleton<WindowEventService>();
- services.AddSingleton<YamlDeserializationService>();
+ services.AddSingleton<JsonService>();
+ services.AddSingleton<YamlService>();
services.AddSingleton<ICommandHandler<NoopCommand>, NoopHandler>();
diff --git a/GlazeWM.Infrastructure/Serialization/JsonService.cs b/GlazeWM.Infrastructure/Serialization/JsonService.cs
new file mode 100644
index 00000000..903571fd
--- /dev/null
+++ b/GlazeWM.Infrastructure/Serialization/JsonService.cs
@@ -0,0 +1,44 @@
+using System.Collections.Generic;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace GlazeWM.Infrastructure.Serialization
+{
+ public class JsonService
+ {
+ private readonly JsonSerializerOptions _jsonSerializerOptions = new()
+ {
+ // TODO: Use built-in snake case policy once support is added.
+ // Ref: https://github.com/dotnet/runtime/issues/782
+ PropertyNamingPolicy = new SnakeCaseNamingPolicy(),
+ NumberHandling = JsonNumberHandling.AllowReadingFromString,
+ IncludeFields = true,
+ Converters = {
+ // Enable strings to be mapped to a C# enum (eg. `BarPosition` enum).
+ new JsonStringEnumConverter()
+ }
+ };
+
+ public string Serialize<T>(T value, List<JsonConverter> converters)
+ {
+ var jsonSerializerOptions = GetJsonSerializerOptions(converters);
+ return JsonSerializer.Serialize(value, jsonSerializerOptions);
+ }
+
+ public T Deserialize<T>(string json, List<JsonConverter> converters)
+ {
+ var jsonDeserializerOptions = GetJsonSerializerOptions(converters);
+ return JsonSerializer.Deserialize<T>(json, jsonDeserializerOptions);
+ }
+
+ private JsonSerializerOptions GetJsonSerializerOptions(List<JsonConverter> converters)
+ {
+ var jsonSerializerOptions = new JsonSerializerOptions(_jsonSerializerOptions);
+
+ foreach (var converter in converters)
+ jsonSerializerOptions.Converters.Add(converter);
+
+ return jsonSerializerOptions;
+ }
+ }
+}
diff --git a/GlazeWM.Infrastructure/Yaml/SnakeCaseNamingPolicy.cs b/GlazeWM.Infrastructure/Serialization/SnakeCaseNamingPolicy.cs
similarity index 92%
rename from GlazeWM.Infrastructure/Yaml/SnakeCaseNamingPolicy.cs
rename to GlazeWM.Infrastructure/Serialization/SnakeCaseNamingPolicy.cs
index 165c7248..21474225 100644
--- a/GlazeWM.Infrastructure/Yaml/SnakeCaseNamingPolicy.cs
+++ b/GlazeWM.Infrastructure/Serialization/SnakeCaseNamingPolicy.cs
@@ -1,7 +1,7 @@
using System.Linq;
using System.Text.Json;
-namespace GlazeWM.Infrastructure.Yaml
+namespace GlazeWM.Infrastructure.Serialization
{
public class SnakeCaseNamingPolicy : JsonNamingPolicy
{
diff --git a/GlazeWM.Infrastructure/Serialization/YamlService.cs b/GlazeWM.Infrastructure/Serialization/YamlService.cs
new file mode 100644
index 00000000..38908811
--- /dev/null
+++ b/GlazeWM.Infrastructure/Serialization/YamlService.cs
@@ -0,0 +1,33 @@
+using System.Collections.Generic;
+using System.IO;
+using System.Text.Json.Serialization;
+using YamlDotNet.Serialization;
+
+namespace GlazeWM.Infrastructure.Serialization
+{
+ public class YamlService
+ {
+ private readonly JsonService _jsonService;
+ private readonly IDeserializer _yamlDeserializer = new DeserializerBuilder().Build();
+
+ public YamlService(JsonService jsonService)
+ {
+ _jsonService = jsonService;
+ }
+
+ /// <summary>
+ /// The YAML deserialization library doesn't have support for polymorphic objects. Because of
+ /// this, the YAML is first converted into JSON and then deserialized via `System.Text.Json`.
+ /// </summary>
+ public T Deserialize<T>(string input, List<JsonConverter> converters)
+ {
+ // Deserializes YAML into key-value pairs (ie. not an object of type `T`).
+ var yamlObject = _yamlDeserializer.Deserialize(new StringReader(input));
+
+ // Convert key-value pairs into a JSON string.
+ var jsonString = _jsonService.Serialize(yamlObject, converters);
+
+ return _jsonService.Deserialize<T>(jsonString, converters);
+ }
+ }
+}
diff --git a/GlazeWM.Infrastructure/Yaml/YamlDeserializationService.cs b/GlazeWM.Infrastructure/Yaml/YamlDeserializationService.cs
deleted file mode 100644
index eaabf787..00000000
--- a/GlazeWM.Infrastructure/Yaml/YamlDeserializationService.cs
+++ /dev/null
@@ -1,53 +0,0 @@
-using System.Collections.Generic;
-using System.IO;
-using System.Text.Json;
-using System.Text.Json.Serialization;
-using YamlDotNet.Serialization;
-
-namespace GlazeWM.Infrastructure.Yaml
-{
- public class YamlDeserializationService
- {
- private readonly IDeserializer _yamlDeserializer = new DeserializerBuilder()
- .Build();
-
- private readonly ISerializer _jsonSerializer = new SerializerBuilder()
- .JsonCompatible()
- .Build();
-
- /// <summary>
- /// The YAML deserializing library doesn't have support for polymorphic objects. Because of
- /// this, the YAML is first converted into JSON and then deserialized via `System.Text.Json`.
- /// </summary>
- public T Deserialize<T>(TextReader input, List<JsonConverter> converters)
- {
- var yamlObject = _yamlDeserializer.Deserialize(input);
- var jsonString = _jsonSerializer.Serialize(yamlObject);
-
- var jsonDeserializerSettings = GetJsonDeserializerOptions(converters);
- return JsonSerializer.Deserialize<T>(jsonString, jsonDeserializerSettings);
- }
-
- private static JsonSerializerOptions GetJsonDeserializerOptions(
- List<JsonConverter> converters
- )
- {
- var jsonDeserializerOptions = new JsonSerializerOptions()
- {
- // TODO: Use built-in snake case policy once support is added.
- // Ref: https://github.com/dotnet/runtime/issues/782
- PropertyNamingPolicy = new SnakeCaseNamingPolicy(),
- NumberHandling = JsonNumberHandling.AllowReadingFromString,
- IncludeFields = true,
- };
-
- foreach (var converter in converters)
- jsonDeserializerOptions.Converters.Add(converter);
-
- // Enable strings to be mapped to a C# enum (eg. `BarPosition` enum).
- jsonDeserializerOptions.Converters.Add(new JsonStringEnumConverter());
-
- return jsonDeserializerOptions;
- }
- }
-}