diff --git a/client/Pack.cs b/client/Pack.cs index f2a7ceb..7040e6e 100644 --- a/client/Pack.cs +++ b/client/Pack.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using System.Text.Json; +using System.Text.Json.Serialization; using OrangeGuidanceTomestone.Helpers; namespace OrangeGuidanceTomestone; @@ -7,12 +8,17 @@ namespace OrangeGuidanceTomestone; public class Pack { internal static SemaphoreSlim AllMutex { get; } = new(1, 1); internal static Pack[] All { get; set; } = []; + private static readonly JsonSerializerOptions Options = new() { + Converters = { + new TemplateConverter(), + }, + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, + }; public string Name { get; init; } public Guid Id { get; init; } - [JsonConverter(typeof(TemplateConverter))] - public ITemplate[] Templates { get; init; } + public Template[] Templates { get; init; } public string[]? Conjunctions { get; init; } public List? Words { get; init; } @@ -21,7 +27,7 @@ public class Pack { Task.Run(async () => { var resp = await ServerHelper.SendRequest(null, HttpMethod.Get, "/packs"); var json = await resp.Content.ReadAsStringAsync(); - var packs = JsonConvert.DeserializeObject(json)!; + var packs = JsonSerializer.Deserialize(json, Pack.Options)!; await AllMutex.WaitAsync(); try { All = packs; @@ -32,48 +38,50 @@ public class Pack { } } -public interface ITemplate { - public string Template { get; } - public string[]? Words { get; } +public class Template { + [JsonPropertyName("template")] + public string Text { get; init; } + public string[]? Words { get; init; } } -public class BasicTemplate : ITemplate { - public string Template { get; init; } - public string[]? Words => null; -} - -[Serializable] -public class WordListTemplate : ITemplate { - public string Template { get; init; } - public string[] Words { get; init; } -} - -public class TemplateConverter : JsonConverter -{ - public override ITemplate? ReadJson(JsonReader reader, Type objectType, ITemplate? existingValue, bool hasExistingValue, JsonSerializer serializer) { - if (reader.TokenType == JsonToken.String) { - var template = reader.ReadAsString(); - if (template == null) { - return null; +public class TemplateConverter : JsonConverter