GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(domain): handle deserialization of polymorphic bar component configs
Lars Bergerd09b058062b8
diff --git a/GlazeWM.Domain/UserConfigs/BarComponentConfig.cs b/GlazeWM.Domain/UserConfigs/BarComponentConfig.csindex ce803b67..53dbf61c 100644--- a/GlazeWM.Domain/UserConfigs/BarComponentConfig.cs+++ b/GlazeWM.Domain/UserConfigs/BarComponentConfig.cs@@ -1,4 +1,3 @@-using System;using System.ComponentModel.DataAnnotations;namespace GlazeWM.Domain.UserConfigs@@ -9,30 +8,5 @@ namespace GlazeWM.Domain.UserConfigspublic string Type { get; set; }public string Margin { get; set; } = "0 10 0 0";-- public string TimeFormatting { get; set; }-- /// <summary>- /// The YAML parsing library doesn't have proper support for polymorphic objects. Therefore,- /// a base class with all properties of derived classes is needed, which is then upcasted into- /// the correct type.- /// </summary>- public BarComponentConfig Upcast()- {- switch (Type)- {- case "workspaces":- return new WorkspacesComponentConfig();-- case "clock":- return new ClockComponentConfig- {- TimeFormatting = TimeFormatting,- };-- default:- throw new ArgumentException();- }- }}}diff --git a/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.csnew file mode 100644index 00000000..bbc20503--- /dev/null+++ b/GlazeWM.Domain/UserConfigs/BarComponentConfigConverter.cs@@ -0,0 +1,38 @@+using System;+using Newtonsoft.Json;+using Newtonsoft.Json.Linq;++namespace GlazeWM.Domain.UserConfigs+{+ public class BarComponentConfigConverter : JsonConverter+ {+ public override bool CanConvert(Type type) => type.IsAssignableFrom(typeof(BarComponentConfig));++ public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)+ {+ var jObject = JObject.Load(reader);++ // Get the type of workspace component config.+ var type = jObject["Type"].Value<string>();++ object target = type switch+ {+ "workspaces" => new WorkspacesComponentConfig(),+ "clock" => new ClockComponentConfig(),+ _ => throw new ArgumentException(),+ };++ serializer.Populate(jObject.CreateReader(), target);++ return target;+ }++ /// <summary>+ /// Serializing is not needed, so it's fine to leave it unimplemented.+ /// </summary>+ public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)+ {+ throw new NotImplementedException();+ }+ }+}diff --git a/GlazeWM.Domain/UserConfigs/BarConfig.cs b/GlazeWM.Domain/UserConfigs/BarConfig.csindex c7691d2a..b7a850d1 100644--- a/GlazeWM.Domain/UserConfigs/BarConfig.cs+++ b/GlazeWM.Domain/UserConfigs/BarConfig.cs@@ -1,5 +1,5 @@using System.Collections.Generic;-using System.Linq;+using Newtonsoft.Json;namespace GlazeWM.Domain.UserConfigs{@@ -9,15 +9,13 @@ namespace GlazeWM.Domain.UserConfigspublic double Opacity { get; set; } = 0.9;- List<BarComponentConfig> _componentsLeft;- public List<BarComponentConfig> ComponentsLeft- {- get { return _componentsLeft; }- set { _componentsLeft = value.Select(configs => configs.Upcast()).ToList(); }- }+ [JsonProperty(ItemConverterType = typeof(BarComponentConfigConverter))]+ public List<BarComponentConfig> ComponentsLeft { get; set; } = new List<BarComponentConfig>();+ [JsonProperty(ItemConverterType = typeof(BarComponentConfigConverter))]public List<BarComponentConfig> ComponentsCenter { get; set; } = new List<BarComponentConfig>();+ [JsonProperty(ItemConverterType = typeof(BarComponentConfigConverter))]public List<BarComponentConfig> ComponentsRight { get; set; } = new List<BarComponentConfig>();}}