From acea7b2e728787a0d83bbf83c8c1f042d2c32e7e Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Mon, 3 Jun 2024 10:15:45 +0800 Subject: + plugins project --- .../Serialization/VerticalAlignmentConverter.cs | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/VerticalAlignmentConverter.cs (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/VerticalAlignmentConverter.cs') diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/VerticalAlignmentConverter.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/VerticalAlignmentConverter.cs new file mode 100644 index 0000000..bc55cda --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Serialization/VerticalAlignmentConverter.cs @@ -0,0 +1,32 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace MonoGame.Extended.Gui.Serialization; + +public class VerticalAlignmentConverter : JsonConverter +{ + /// + public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(VerticalAlignment); + + /// + public override VerticalAlignment Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var value = reader.GetString(); + + if (value.Equals("Center", StringComparison.OrdinalIgnoreCase) || value.Equals("Centre", StringComparison.OrdinalIgnoreCase)) + { + return VerticalAlignment.Centre; + } + + if (Enum.TryParse(value, true, out var alignment)) + { + return alignment; + } + + throw new InvalidOperationException($"Invalid value for '{nameof(VerticalAlignment)}'"); + } + + /// + public override void Write(Utf8JsonWriter writer, VerticalAlignment value, JsonSerializerOptions options) { } +} -- cgit v1.1-26-g67d0