< Summary

Class:Microsoft.Azure.Search.Serialization.SearchResultConverter`1
Assembly:Microsoft.Azure.Search.Data
File(s):C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Serialization\SearchResultConverter.cs
Covered lines:15
Uncovered lines:2
Coverable lines:17
Total lines:61
Line coverage:88.2% (15 of 17)
Covered branches:2
Total branches:2
Branch coverage:100% (2 of 2)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_CanRead()-100%100%
get_CanWrite()-0%100%
CanConvert(...)-100%100%
ReadJson(...)-100%100%
WriteJson(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Serialization\SearchResultConverter.cs

#LineLine coverage
 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
 5namespace 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    {
 2514222        public override bool CanRead => true;
 23
 024        public override bool CanWrite => false;
 25
 26        public override bool CanConvert(Type objectType) =>
 16739027            typeof(SearchResult<T>).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
 28
 29        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer seriali
 30        {
 2514231            JObject propertyBag = serializer.Deserialize<JObject>(reader);
 32
 33            HitHighlights DeserializeHighlights()
 34            {
 2513435                JToken highlights = propertyBag["@search.highlights"];
 36
 2513437                if (highlights != null)
 38                {
 439                    var highlightReader = new JTokenReader(highlights);
 440                    return serializer.Deserialize<HitHighlights>(highlightReader);
 41                }
 42
 2513043                return null;
 44            }
 45
 46            T DeserializeDocument()
 47            {
 2514248                var docReader = new JTokenReader(propertyBag);
 2514249                return serializer.Deserialize<T>(docReader);
 50            }
 51
 2514252            JToken score = propertyBag["@search.score"];
 2514253            return new SearchResult<T>(
 2514254                document: DeserializeDocument(),
 2514255                score: score.Value<double>(),
 2514256                highlights: DeserializeHighlights());
 57        }
 58
 059        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImple
 60    }
 61}