| | | 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 | | using System; |
| | | 6 | | using System.Globalization; |
| | | 7 | | using System.Reflection; |
| | | 8 | | using Newtonsoft.Json; |
| | | 9 | | |
| | | 10 | | namespace 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) => |
| | 265292 | 27 | | typeof(double?).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()) || |
| | 265292 | 28 | | typeof(double).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()); |
| | | 29 | | |
| | | 30 | | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer seriali |
| | | 31 | | { |
| | 244 | 32 | | if (reader.TokenType == JsonToken.Null) |
| | | 33 | | { |
| | 6 | 34 | | return null; |
| | | 35 | | } |
| | | 36 | | |
| | 238 | 37 | | if (reader.TokenType == JsonToken.String) |
| | | 38 | | { |
| | 14 | 39 | | string strValue = (string)reader.Value; |
| | | 40 | | |
| | | 41 | | switch (strValue) |
| | | 42 | | { |
| | 4 | 43 | | case ODataPositiveInfinity: return Double.PositiveInfinity; |
| | 4 | 44 | | case ODataNegativeInfinity: return Double.NegativeInfinity; |
| | 6 | 45 | | 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. |
| | 224 | 50 | | return Convert.ToDouble(reader.Value); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
| | | 54 | | { |
| | 1246 | 55 | | if (value == null) |
| | | 56 | | { |
| | 0 | 57 | | writer.WriteNull(); |
| | | 58 | | } |
| | | 59 | | else |
| | | 60 | | { |
| | 1246 | 61 | | double doubleValue = (double)value; |
| | | 62 | | |
| | 1246 | 63 | | if (Double.IsNegativeInfinity(doubleValue)) |
| | | 64 | | { |
| | 6 | 65 | | writer.WriteValue(ODataNegativeInfinity); |
| | | 66 | | } |
| | 1240 | 67 | | else if (Double.IsPositiveInfinity(doubleValue)) |
| | | 68 | | { |
| | 6 | 69 | | writer.WriteValue(ODataPositiveInfinity); |
| | | 70 | | } |
| | | 71 | | else |
| | | 72 | | { |
| | 1234 | 73 | | writer.WriteValue(doubleValue); |
| | | 74 | | } |
| | | 75 | | } |
| | 1234 | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |