| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Serialization |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Reflection; |
| | 9 | | using Microsoft.Azure.Search.Models; |
| | 10 | | using Newtonsoft.Json; |
| | 11 | | using Newtonsoft.Json.Converters; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Serializes IndexAction instances so that the JSON is OData-compliant. |
| | 15 | | /// </summary> |
| | 16 | | /// <typeparam name="T"> |
| | 17 | | /// The CLR type that maps to the index schema. Instances of this type can be stored as documents in the index. |
| | 18 | | /// </typeparam> |
| | 19 | | internal class IndexActionConverter<T> : JsonConverter |
| | 20 | | { |
| 0 | 21 | | public override bool CanRead => false; |
| | 22 | |
|
| 27508 | 23 | | public override bool CanWrite => true; |
| | 24 | |
|
| | 25 | | public override bool CanConvert(Type objectType) |
| | 26 | | { |
| 124988 | 27 | | return typeof(IndexAction<T>).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()); |
| | 28 | | } |
| | 29 | |
|
| | 30 | | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer seriali |
| | 31 | | { |
| 0 | 32 | | throw new NotImplementedException(); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
| | 36 | | { |
| 27508 | 37 | | var action = (IndexAction<T>)value; |
| | 38 | |
|
| | 39 | | void WriteActionAnnotation(JsonWriter innerWriter) |
| | 40 | | { |
| 27508 | 41 | | innerWriter.WritePropertyName("@search.action"); |
| | 42 | |
|
| 27508 | 43 | | var converter = new StringEnumConverter(); |
| 27508 | 44 | | converter.WriteJson(innerWriter, action.ActionType, serializer); |
| 27508 | 45 | | } |
| | 46 | |
|
| 27508 | 47 | | var injectingWriter = new InjectingJsonWriter(writer) { OnStart = WriteActionAnnotation }; |
| 27508 | 48 | | serializer.Serialize(injectingWriter, action.Document); |
| 27508 | 49 | | } |
| | 50 | | } |
| | 51 | | } |