A System.Text.Json converter that deserializes a JSON integer representing milliseconds since the Unix epoch into a DateTime. It implements Read to convert the Int64 milliseconds into a DateTime, Write is not implemented.
#nullable enable annotations
using System.Text.Json;
using System.Text.Json.Serialization;
namespace LichessNET.Converters;
public class MillisecondUnixConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var milliseconds = reader.GetInt64();
DateTime unixEpoch = DateTime.UnixEpoch;
return unixEpoch.Add(TimeSpan.FromMilliseconds(milliseconds));
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}