/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. *--------------------------------------------------------------------------------------------*/ using System.ComponentModel; using System.Text.Json; using System.Text.Json.Serialization; namespace GitHub.Copilot; /// Converts between JSON numeric milliseconds-since-Unix-epoch and . [EditorBrowsable(EditorBrowsableState.Never)] public sealed class UnixMillisecondsDateTimeOffsetConverter : JsonConverter { /// public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { // The CLI may serialize the epoch-millisecond timestamp as a JSON integer // or as a floating-point number (e.g. 1700000000000.0). GetInt64 throws on a // fractional token, so fall back to reading a double and truncating. long milliseconds = reader.TryGetInt64(out long value) ? value : (long)reader.GetDouble(); return DateTimeOffset.FromUnixTimeMilliseconds(milliseconds); } /// public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) => writer.WriteNumberValue(value.ToUnixTimeMilliseconds()); }