|
| 1 | +// <copyright file="SpecialNumberConverter.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 | +// Serializes and deserializes double into a BiDi spec-compliant number value. |
| 27 | +// See https://w3c.github.io/webdriver-bidi/#type-script-PrimitiveProtocolValue |
| 28 | +internal sealed class SpecialNumberConverter : JsonConverter<double> |
| 29 | +{ |
| 30 | + public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 31 | + { |
| 32 | + switch (reader.TokenType) |
| 33 | + { |
| 34 | + case JsonTokenType.Number: |
| 35 | + return reader.GetDouble(); |
| 36 | + |
| 37 | + case JsonTokenType.String: |
| 38 | + var str = reader.GetString()!; |
| 39 | + if (str.Equals("-0", StringComparison.Ordinal)) |
| 40 | + { |
| 41 | + return -0.0; |
| 42 | + } |
| 43 | + |
| 44 | + if (str.Equals("NaN", StringComparison.Ordinal)) |
| 45 | + { |
| 46 | + return double.NaN; |
| 47 | + } |
| 48 | + |
| 49 | + if (str.Equals("Infinity", StringComparison.Ordinal)) |
| 50 | + { |
| 51 | + return double.PositiveInfinity; |
| 52 | + } |
| 53 | + |
| 54 | + if (str.Equals("-Infinity", StringComparison.Ordinal)) |
| 55 | + { |
| 56 | + return double.NegativeInfinity; |
| 57 | + } |
| 58 | + |
| 59 | + throw new JsonException($"JSON '{str}' string could not be parsed to a special number"); |
| 60 | + |
| 61 | + default: |
| 62 | + throw new JsonException($"JSON type not a number or string: {reader.TokenType}"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) |
| 67 | + { |
| 68 | + if (double.IsNaN(value)) |
| 69 | + { |
| 70 | + writer.WriteStringValue("NaN"); |
| 71 | + } |
| 72 | + else if (double.IsPositiveInfinity(value)) |
| 73 | + { |
| 74 | + writer.WriteStringValue("Infinity"); |
| 75 | + } |
| 76 | + else if (double.IsNegativeInfinity(value)) |
| 77 | + { |
| 78 | + writer.WriteStringValue("-Infinity"); |
| 79 | + } |
| 80 | + else if (IsNegativeZero(value)) |
| 81 | + { |
| 82 | + writer.WriteStringValue("-0"); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + writer.WriteNumberValue(value); |
| 87 | + } |
| 88 | + |
| 89 | + static bool IsNegativeZero(double x) |
| 90 | + { |
| 91 | + // Negative zero is less trivial to test, because 0 == -0 is true |
| 92 | + // We need to do a bit pattern comparison |
| 93 | + |
| 94 | + return BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(-0.0); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments