| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text.Json; |
| | 6 | | using System.Text.Json.Serialization; |
| | 7 | | using Azure.Core; |
| | 8 | |
|
| | 9 | | namespace Azure.Search.Documents.Models |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Convert JSON to and from a SearchDocument. |
| | 13 | | /// </summary> |
| | 14 | | internal class SearchDocumentConverter : JsonConverter<SearchDocument> |
| | 15 | | { |
| 1 | 16 | | public static SearchDocumentConverter Shared { get; } = |
| 1 | 17 | | new SearchDocumentConverter(); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Write a SearchDocument as JSON. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="writer">The JSON writer.</param> |
| | 23 | | /// <param name="value">The document.</param> |
| | 24 | | /// <param name="options">Serialization options.</param> |
| | 25 | | public override void Write( |
| | 26 | | Utf8JsonWriter writer, |
| | 27 | | SearchDocument value, |
| | 28 | | JsonSerializerOptions options) |
| | 29 | | { |
| 16670 | 30 | | Argument.AssertNotNull(writer, nameof(writer)); |
| 16670 | 31 | | Argument.AssertNotNull(value, nameof(value)); |
| 16670 | 32 | | Argument.AssertNotNull(options, nameof(options)); |
| 16670 | 33 | | JsonSerialization.WriteSearchDocument( |
| 16670 | 34 | | writer, |
| 16670 | 35 | | value, |
| 16670 | 36 | | options); |
| 16670 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Parse JSON into a SearchDocument. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="reader">The JSON reader.</param> |
| | 43 | | /// <param name="typeToConvert">The type to convert to.</param> |
| | 44 | | /// <param name="options">Serialization options.</param> |
| | 45 | | /// <returns>A deserialized SearchDocument.</returns> |
| | 46 | | public override SearchDocument Read( |
| | 47 | | ref Utf8JsonReader reader, |
| | 48 | | Type typeToConvert, |
| | 49 | | JsonSerializerOptions options) |
| | 50 | | { |
| 8746 | 51 | | Argument.AssertNotNull(typeToConvert, nameof(typeToConvert)); |
| 8746 | 52 | | Argument.AssertNotNull(options, nameof(options)); |
| 8746 | 53 | | return JsonSerialization.ReadSearchDocument( |
| 8746 | 54 | | ref reader, |
| 8746 | 55 | | typeToConvert, |
| 8746 | 56 | | options); |
| | 57 | | } |
| | 58 | | } |
| | 59 | | } |