jellyfin/MediaBrowser.Common/Json/Converters/JsonDateTimeConverter.cs

34 lines
1.1 KiB
C#
Raw Normal View History

2020-12-21 08:12:32 -07:00
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace MediaBrowser.Common.Json.Converters
{
/// <summary>
/// Legacy DateTime converter.
/// Milliseconds aren't output if zero by default.
/// </summary>
public class JsonDateTimeConverter : JsonConverter<DateTime>
{
/// <inheritdoc />
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetDateTime();
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
2020-12-21 08:41:13 -07:00
if (value.Millisecond == 0)
2020-12-21 08:12:32 -07:00
{
// Remaining ticks value will be 0, manually format.
writer.WriteStringValue(value.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffZ", CultureInfo.InvariantCulture));
}
else
{
writer.WriteStringValue(value);
}
}
}
}