< Summary

Class:Azure.Search.Documents.SearchDoubleConverter
Assembly:Azure.Search.Documents
File(s):C:\Git\azure-sdk-for-net\sdk\search\Azure.Search.Documents\src\Serialization\SearchDoubleConverter.cs
Covered lines:17
Uncovered lines:1
Coverable lines:18
Total lines:72
Line coverage:94.4% (17 of 18)
Covered branches:13
Total branches:14
Branch coverage:92.8% (13 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_Shared()-100%100%
.cctor()-100%100%
Read(...)-85.71%87.5%
Write(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Azure.Search.Documents\src\Serialization\SearchDoubleConverter.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Diagnostics;
 6using System.Text.Json;
 7using System.Text.Json.Serialization;
 8using Azure.Core;
 9
 10namespace Azure.Search.Documents
 11{
 12    /// <summary>
 13    /// Convert doubles to and from JSON.  Search allows INF, -INF, and NaN as
 14    /// string values.
 15    /// </summary>
 16    internal class SearchDoubleConverter : JsonConverter<double>
 17    {
 118        public static SearchDoubleConverter Shared { get; } =
 119            new SearchDoubleConverter();
 20
 21        public override double Read(
 22            ref Utf8JsonReader reader,
 23            Type typeToConvert,
 24            JsonSerializerOptions options)
 25        {
 26            Debug.Assert(typeToConvert != null);
 27            Debug.Assert(options != null);
 28
 8229            if (reader.TokenType == JsonTokenType.String)
 30            {
 631                switch (reader.GetString())
 32                {
 33                    case Constants.InfValue:
 234                        return double.PositiveInfinity;
 35                    case Constants.NegativeInfValue:
 236                        return double.NegativeInfinity;
 37                    case Constants.NanValue:
 238                        return double.NaN;
 39                    default:
 040                        throw new JsonException();
 41                }
 42            }
 7643            return reader.GetDouble();
 44        }
 45
 46        public override void Write(
 47            Utf8JsonWriter writer,
 48            double value,
 49            JsonSerializerOptions options)
 50        {
 7051            Argument.AssertNotNull(writer, nameof(writer));
 52            Debug.Assert(options != null);
 53
 7054            if (double.IsPositiveInfinity(value))
 55            {
 256                writer.WriteStringValue(Constants.InfValue);
 57            }
 6858            else if (double.IsNegativeInfinity(value))
 59            {
 260                writer.WriteStringValue(Constants.NegativeInfValue);
 61            }
 6662            else if (double.IsNaN(value))
 63            {
 864                writer.WriteStringValue(Constants.NanValue);
 65            }
 66            else
 67            {
 5868                writer.WriteNumberValue(value);
 69            }
 5870        }
 71    }
 72}