|
| 1 | +// <copyright file="DoubleConverter.cs" company="Selenium Committers"> |
| 2 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The SFC licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// </copyright> |
| 19 | + |
| 20 | +using System; |
| 21 | +using System.Text.Json; |
| 22 | +using System.Text.Json.Serialization; |
| 23 | + |
| 24 | +namespace OpenQA.Selenium.BiDi.Communication.Json.Converters |
| 25 | +{ |
| 26 | + internal class DoubleConverter : JsonConverter<double> |
| 27 | + { |
| 28 | + public override double Read(ref Utf8JsonReader reader, System.Type typeToConvert, JsonSerializerOptions options) |
| 29 | + { |
| 30 | + if (reader.TryGetDouble(out double d)) |
| 31 | + { |
| 32 | + return d; |
| 33 | + } |
| 34 | + |
| 35 | + var str = reader.GetString() ?? throw new JsonException(); |
| 36 | + |
| 37 | + if (str.Equals("-0", System.StringComparison.Ordinal)) |
| 38 | + { |
| 39 | + return -0.0; |
| 40 | + } |
| 41 | + else if (str.Equals("NaN", System.StringComparison.Ordinal)) |
| 42 | + { |
| 43 | + return double.NaN; |
| 44 | + } |
| 45 | + else if (str.Equals("Infinity", System.StringComparison.Ordinal)) |
| 46 | + { |
| 47 | + return double.PositiveInfinity; |
| 48 | + } |
| 49 | + else if (str.Equals("-Infinity", System.StringComparison.Ordinal)) |
| 50 | + { |
| 51 | + return double.NegativeInfinity; |
| 52 | + } |
| 53 | + |
| 54 | + throw new JsonException(); |
| 55 | + } |
| 56 | + |
| 57 | + public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) |
| 58 | + { |
| 59 | + if (double.IsNaN(value)) |
| 60 | + { |
| 61 | + writer.WriteStringValue("NaN"); |
| 62 | + } |
| 63 | + else if (double.IsPositiveInfinity(value)) |
| 64 | + { |
| 65 | + writer.WriteStringValue("Infinity"); |
| 66 | + } |
| 67 | + else if (double.IsNegativeInfinity(value)) |
| 68 | + { |
| 69 | + writer.WriteStringValue("-Infinity"); |
| 70 | + } |
| 71 | + else if (IsNegativeZero(value)) |
| 72 | + { |
| 73 | + writer.WriteStringValue("-0"); |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + writer.WriteNumberValue(value); |
| 78 | + } |
| 79 | + |
| 80 | + static bool IsNegativeZero(double x) |
| 81 | + { |
| 82 | + const long NegativeZeroBits = -9223372036854775808; |
| 83 | + |
| 84 | + return BitConverter.DoubleToInt64Bits(x) == NegativeZeroBits; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments