28 lines
803 B
C#
28 lines
803 B
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Messenger.Api.Services;
|
|
|
|
public sealed class UtcDateTimeOffsetJsonConverter : JsonConverter<DateTimeOffset>
|
|
{
|
|
public override DateTimeOffset Read(
|
|
ref Utf8JsonReader reader,
|
|
Type typeToConvert,
|
|
JsonSerializerOptions options)
|
|
{
|
|
return DateTimeOffset.Parse(
|
|
reader.GetString()!,
|
|
CultureInfo.InvariantCulture,
|
|
DateTimeStyles.RoundtripKind);
|
|
}
|
|
|
|
public override void Write(
|
|
Utf8JsonWriter writer,
|
|
DateTimeOffset value,
|
|
JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStringValue(
|
|
value.UtcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'", CultureInfo.InvariantCulture));
|
|
}
|
|
} |