| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Text.Json; |
| | 8 | | using System.Text.Json.Serialization; |
| | 9 | | using Azure.Core; |
| | 10 | |
|
| | 11 | | namespace Azure.Search.Documents |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Convert dates to and from JSON. They're expected to be in 8601 UTC. |
| | 15 | | /// </summary> |
| | 16 | | internal class SearchDateTimeOffsetConverter : JsonConverter<DateTimeOffset> |
| | 17 | | { |
| 1 | 18 | | public static SearchDateTimeOffsetConverter Shared { get; } = |
| 1 | 19 | | new SearchDateTimeOffsetConverter(); |
| | 20 | |
|
| | 21 | | public override DateTimeOffset Read( |
| | 22 | | ref Utf8JsonReader reader, |
| | 23 | | Type typeToConvert, |
| | 24 | | JsonSerializerOptions options) |
| | 25 | | { |
| | 26 | | Debug.Assert(typeToConvert != null); |
| | 27 | | Debug.Assert(options != null); |
| | 28 | |
|
| 136 | 29 | | string text = reader.GetString(); |
| 136 | 30 | | return DateTimeOffset.Parse(text, CultureInfo.InvariantCulture); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public override void Write( |
| | 34 | | Utf8JsonWriter writer, |
| | 35 | | DateTimeOffset value, |
| | 36 | | JsonSerializerOptions options) |
| | 37 | | { |
| 28 | 38 | | Argument.AssertNotNull(writer, nameof(writer)); |
| | 39 | | Debug.Assert(options != null); |
| 28 | 40 | | writer.WriteStringValue(JsonSerialization.Date(value, CultureInfo.InvariantCulture)); |
| 28 | 41 | | } |
| | 42 | | } |
| | 43 | | } |