mirror of https://github.com/jellyfin/jellyfin.git
Merge pull request #4842 from crobibero/date-time-formatter
Add JsonDateTimeConverter
This commit is contained in:
commit
c8a89b0fe3
|
@ -0,0 +1,34 @@
|
|||
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)
|
||||
{
|
||||
if (value.Millisecond == 0)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -44,6 +44,7 @@ namespace MediaBrowser.Common.Json
|
|||
options.Converters.Add(new JsonStringEnumConverter());
|
||||
options.Converters.Add(new JsonNullableStructConverterFactory());
|
||||
options.Converters.Add(new JsonBoolNumberConverter());
|
||||
options.Converters.Add(new JsonDateTimeConverter());
|
||||
|
||||
return options;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue