| | | 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.Linq; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Deserializes SuggestResult instances from OData-compliant JSON. |
| | | 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 SuggestResultConverter<T> : JsonConverter |
| | | 20 | | { |
| | 108 | 21 | | public override bool CanRead => true; |
| | | 22 | | |
| | 0 | 23 | | public override bool CanWrite => false; |
| | | 24 | | |
| | | 25 | | public override bool CanConvert(Type objectType) => |
| | 167224 | 26 | | typeof(SuggestResult<T>).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()); |
| | | 27 | | |
| | | 28 | | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer seriali |
| | | 29 | | { |
| | 108 | 30 | | JObject propertyBag = serializer.Deserialize<JObject>(reader); |
| | 108 | 31 | | JToken text = propertyBag["@search.text"]; |
| | | 32 | | |
| | 108 | 33 | | var docReader = new JTokenReader(propertyBag); |
| | 108 | 34 | | return new SuggestResult<T>(document: serializer.Deserialize<T>(docReader), text: text.Value<string>()); |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImple |
| | | 38 | | } |
| | | 39 | | } |