| | 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 | | using HitHighlights = System.Collections.Generic.IDictionary<string, System.Collections.Generic.IList<string>>; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Deserializes SearchResult instances from OData-compliant JSON. |
| | 16 | | /// </summary> |
| | 17 | | /// <typeparam name="T"> |
| | 18 | | /// The CLR type that maps to the index schema. Instances of this type can be stored as documents in the index. |
| | 19 | | /// </typeparam> |
| | 20 | | internal class SearchResultConverter<T> : JsonConverter |
| | 21 | | { |
| 25142 | 22 | | public override bool CanRead => true; |
| | 23 | |
|
| 0 | 24 | | public override bool CanWrite => false; |
| | 25 | |
|
| | 26 | | public override bool CanConvert(Type objectType) => |
| 167390 | 27 | | typeof(SearchResult<T>).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()); |
| | 28 | |
|
| | 29 | | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer seriali |
| | 30 | | { |
| 25142 | 31 | | JObject propertyBag = serializer.Deserialize<JObject>(reader); |
| | 32 | |
|
| | 33 | | HitHighlights DeserializeHighlights() |
| | 34 | | { |
| 25134 | 35 | | JToken highlights = propertyBag["@search.highlights"]; |
| | 36 | |
|
| 25134 | 37 | | if (highlights != null) |
| | 38 | | { |
| 4 | 39 | | var highlightReader = new JTokenReader(highlights); |
| 4 | 40 | | return serializer.Deserialize<HitHighlights>(highlightReader); |
| | 41 | | } |
| | 42 | |
|
| 25130 | 43 | | return null; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | T DeserializeDocument() |
| | 47 | | { |
| 25142 | 48 | | var docReader = new JTokenReader(propertyBag); |
| 25142 | 49 | | return serializer.Deserialize<T>(docReader); |
| | 50 | | } |
| | 51 | |
|
| 25142 | 52 | | JToken score = propertyBag["@search.score"]; |
| 25142 | 53 | | return new SearchResult<T>( |
| 25142 | 54 | | document: DeserializeDocument(), |
| 25142 | 55 | | score: score.Value<double>(), |
| 25142 | 56 | | highlights: DeserializeHighlights()); |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImple |
| | 60 | | } |
| | 61 | | } |