From fbfc01c7f3c1a0a684c7990f561484facc7d07e1 Mon Sep 17 00:00:00 2001 From: Toastie Date: Sun, 31 Mar 2024 23:45:30 +1300 Subject: [PATCH] Added jsonconverters to Ellie --- .../Common/JsonConverters/CultureInfoConverter.cs | 14 ++++++++++++++ .../Common/JsonConverters/Rgba32Converter.cs | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/EllieBot/Common/JsonConverters/CultureInfoConverter.cs create mode 100644 src/EllieBot/Common/JsonConverters/Rgba32Converter.cs diff --git a/src/EllieBot/Common/JsonConverters/CultureInfoConverter.cs b/src/EllieBot/Common/JsonConverters/CultureInfoConverter.cs new file mode 100644 index 0000000..0c26f15 --- /dev/null +++ b/src/EllieBot/Common/JsonConverters/CultureInfoConverter.cs @@ -0,0 +1,14 @@ +using System.Globalization; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace EllieBot.Common.JsonConverters; + +public class CultureInfoConverter : JsonConverter +{ + public override CultureInfo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => new(reader.GetString() ?? "en-US"); + + public override void Write(Utf8JsonWriter writer, CultureInfo value, JsonSerializerOptions options) + => writer.WriteStringValue(value.Name); +} \ No newline at end of file diff --git a/src/EllieBot/Common/JsonConverters/Rgba32Converter.cs b/src/EllieBot/Common/JsonConverters/Rgba32Converter.cs new file mode 100644 index 0000000..ef619a6 --- /dev/null +++ b/src/EllieBot/Common/JsonConverters/Rgba32Converter.cs @@ -0,0 +1,14 @@ +using SixLabors.ImageSharp.PixelFormats; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace EllieBot.Common.JsonConverters; + +public class Rgba32Converter : JsonConverter +{ + public override Rgba32 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + => Rgba32.ParseHex(reader.GetString()); + + public override void Write(Utf8JsonWriter writer, Rgba32 value, JsonSerializerOptions options) + => writer.WriteStringValue(value.ToHex()); +} \ No newline at end of file