< Summary

Class:Microsoft.Azure.Search.Serialization.EdmDoubleConverter
Assembly:Microsoft.Azure.Search.Data
File(s):C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Serialization\EdmDoubleConverter.cs
Covered lines:18
Uncovered lines:1
Coverable lines:19
Total lines:78
Line coverage:94.7% (18 of 19)
Covered branches:15
Total branches:16
Branch coverage:93.7% (15 of 16)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
CanConvert(...)-100%100%
ReadJson(...)-100%100%
WriteJson(...)-88.89%83.33%

File(s)

C:\Git\azure-sdk-for-net\sdk\search\Microsoft.Azure.Search.Data\src\Customizations\Serialization\EdmDoubleConverter.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
 5using System;
 6using System.Globalization;
 7using System.Reflection;
 8using Newtonsoft.Json;
 9
 10namespace Microsoft.Azure.Search.Serialization
 11{
 12    /// <summary>
 13    /// Serializes doubles to and from the OData EDM wire format.
 14    /// </summary>
 15    /// <remarks>
 16    /// This JSON converter treats all but three <c cref="System.Double">System.Double</c> values as JSON numbers. The t
 17    /// are <c cref="System.Double.NaN">NaN</c>, which converts to and from the JSON string "NaN",
 18    /// <c cref="System.Double.PositiveInfinity">PositiveInfinity</c>, which converts to and from the JSON string "INF",
 19    /// <c cref="System.Double.NegativeInfinity">NegativeInfinity</c>, which converts to and from the JSON string "-INF"
 20    /// </remarks>
 21    internal class EdmDoubleConverter : JsonConverter
 22    {
 23        private const string ODataNegativeInfinity = "-INF";
 24        private const string ODataPositiveInfinity = "INF";
 25
 26        public override bool CanConvert(Type objectType) =>
 26529227            typeof(double?).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()) ||
 26529228            typeof(double).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
 29
 30        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer seriali
 31        {
 24432            if (reader.TokenType == JsonToken.Null)
 33            {
 634                return null;
 35            }
 36
 23837            if (reader.TokenType == JsonToken.String)
 38            {
 1439                string strValue = (string)reader.Value;
 40
 41                switch (strValue)
 42                {
 443                    case ODataPositiveInfinity: return Double.PositiveInfinity;
 444                    case ODataNegativeInfinity: return Double.NegativeInfinity;
 645                    default: return Double.Parse(strValue, CultureInfo.InvariantCulture);
 46                }
 47            }
 48
 49            // We can't use a direct cast because sometimes we get integers from the reader.
 22450            return Convert.ToDouble(reader.Value);
 51        }
 52
 53        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 54        {
 124655            if (value == null)
 56            {
 057                writer.WriteNull();
 58            }
 59            else
 60            {
 124661                double doubleValue = (double)value;
 62
 124663                if (Double.IsNegativeInfinity(doubleValue))
 64                {
 665                    writer.WriteValue(ODataNegativeInfinity);
 66                }
 124067                else if (Double.IsPositiveInfinity(doubleValue))
 68                {
 669                    writer.WriteValue(ODataPositiveInfinity);
 70                }
 71                else
 72                {
 123473                    writer.WriteValue(doubleValue);
 74                }
 75            }
 123476        }
 77    }
 78}