Code/Converters/GameStatsConverter.cs

A System.Text.Json converter that deserializes a JSON object mapping Lichess gamemode names to game stats. It parses the document, maps each property name to a Gamemode enum, detects whether the value represents GamemodeStats (contains "games") or RunStats (contains "runs"), deserializes into the appropriate IGameStats implementation, and returns a Dictionary<Gamemode, IGameStats>. The Write method is not implemented.

Http CallsNetworkingFile Access
#nullable enable annotations

using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using LichessNET.Entities.Enumerations;
using LichessNET.Entities.Game;
using LichessNET.Entities.Interfaces;
using LichessNET.Entities.Stats;

namespace LichessNET.Converters;

public class GameStatsConverter : JsonConverter<Dictionary<Gamemode, IGameStats>>
{
    public override Dictionary<Gamemode, IGameStats>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        var output = new Dictionary<Gamemode, IGameStats>();
        using (JsonDocument doc = JsonDocument.ParseValue(ref reader))
        {
            foreach (JsonProperty property in doc.RootElement.EnumerateObject())
            {
                if (Gamemode.TryParse(property.Name, true, out Gamemode gamemode))
                {
                    IGameStats? stats = null;
                    if (property.Value.TryGetProperty("games", out _))
                    {
                        stats = JsonSerializer.Deserialize<GamemodeStats>(property.Value, new JsonSerializerOptions(){PropertyNameCaseInsensitive = true});
                    }
                    else if (property.Value.TryGetProperty("runs", out _))
                    {
                        stats = JsonSerializer.Deserialize<RunStats>(property.Value, new JsonSerializerOptions(){PropertyNameCaseInsensitive = true});
                    }

                    if (stats != null)
                    {
                        output.Add(gamemode, stats);
                    }
                }
            }
            
        }

        return output;
    }

    public override void Write(Utf8JsonWriter writer, Dictionary<Gamemode, IGameStats> value, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }
}