Utility class that centralizes System.Text.Json settings and helpers for LichessNET types. It defines shared JsonSerializerOptions with custom converters, provides generic Deserialize and line-delimited deserialization, parses JsonElement, and adds an extension method to read properties into typed values.
#nullable enable annotations
namespace LichessNET.Internal;
internal static class LichessJson
{
public static readonly JsonSerializerOptions Options = new()
{
PropertyNameCaseInsensitive = true,
Converters =
{
new JsonStringEnumConverter( JsonNamingPolicy.CamelCase ),
new LichessNET.Converters.GameStatsConverter(),
new LichessNET.Converters.MillisecondUnixConverter(),
new LichessNET.Converters.PermissionJsonConverter(),
new LichessNET.Converters.SecondsToTimeSpanConverter(),
new LichessNET.Entities.Social.Timeline.TimelineEventDataConverter()
}
};
public static T Deserialize<T>( string json )
{
return JsonSerializer.Deserialize<T>( json, Options );
}
public static List<T> DeserializeLines<T>( string content )
{
var result = new List<T>();
foreach ( var line in content.Split( '\n' ) )
{
if ( string.IsNullOrWhiteSpace( line ) )
continue;
var value = Deserialize<T>( line.Trim() );
if ( value != null )
result.Add( value );
}
return result;
}
public static JsonElement Parse( string json )
{
using var document = JsonDocument.Parse( json );
return document.RootElement.Clone();
}
public static T Get<T>( this JsonElement element, string propertyName )
{
return element.TryGetProperty( propertyName, out var property )
? property.Deserialize<T>( Options )
: default;
}
}