| | 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 SearchDateTimeConverter : JsonConverter<DateTime> |
| | 17 | | { |
| 1 | 18 | | public static SearchDateTimeConverter Shared { get; } = |
| 1 | 19 | | new SearchDateTimeConverter(); |
| | 20 | |
|
| | 21 | | public override DateTime Read( |
| | 22 | | ref Utf8JsonReader reader, |
| | 23 | | Type typeToConvert, |
| | 24 | | JsonSerializerOptions options) |
| | 25 | | { |
| | 26 | | Debug.Assert(typeToConvert != null); |
| | 27 | | Debug.Assert(options != null); |
| | 28 | |
|
| 0 | 29 | | string text = reader.GetString(); |
| 0 | 30 | | return DateTime.Parse(text, CultureInfo.InvariantCulture); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public override void Write( |
| | 34 | | Utf8JsonWriter writer, |
| | 35 | | DateTime value, |
| | 36 | | JsonSerializerOptions options) |
| | 37 | | { |
| 0 | 38 | | Argument.AssertNotNull(writer, nameof(writer)); |
| | 39 | | Debug.Assert(options != null); |
| 0 | 40 | | writer.WriteStringValue(JsonSerialization.Date(value, CultureInfo.InvariantCulture)); |
| 0 | 41 | | } |
| | 42 | | } |
| | 43 | | } |