-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[dotnet] Handle negative zero BiDi response #15898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// <copyright file="BiDiDoubleConverter.cs" company="Selenium Committers"> | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters; | ||
|
||
/// <summary> | ||
nvborisenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Serializes and deserializes <see cref="double"/> into a | ||
/// <see href="https://w3c.github.io/webdriver-bidi/#type-script-PrimitiveProtocolValue">BiDi spec-compliant number value</see>. | ||
/// </summary> | ||
internal sealed class BiDiDoubleConverter : JsonConverter<double> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose I think you are right about I like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have even better proposal:
It makes naming clearer, and it allows to use the same approach for all converters where all of them are still global. I still remember our challenge #15329 (comment). At least in this PR we don't not introduce new knowledge and follows current patterns. |
||
{ | ||
public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TryGetDouble(out double d)) | ||
{ | ||
return d; | ||
} | ||
|
||
var str = reader.GetString() ?? throw new JsonException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a message what is going wrong. |
||
|
||
if (str.Equals("-0", StringComparison.Ordinal)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An in other places please. |
||
{ | ||
return -0.0; | ||
} | ||
else if (str.Equals("NaN", StringComparison.Ordinal)) | ||
{ | ||
return double.NaN; | ||
} | ||
else if (str.Equals("Infinity", StringComparison.Ordinal)) | ||
{ | ||
return double.PositiveInfinity; | ||
} | ||
else if (str.Equals("-Infinity", StringComparison.Ordinal)) | ||
{ | ||
return double.NegativeInfinity; | ||
} | ||
|
||
throw new JsonException(); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options) | ||
{ | ||
if (double.IsNaN(value)) | ||
{ | ||
writer.WriteStringValue("NaN"); | ||
} | ||
else if (double.IsPositiveInfinity(value)) | ||
{ | ||
writer.WriteStringValue("Infinity"); | ||
} | ||
else if (double.IsNegativeInfinity(value)) | ||
{ | ||
writer.WriteStringValue("-Infinity"); | ||
} | ||
else if (IsNegativeZero(value)) | ||
{ | ||
writer.WriteStringValue("-0"); | ||
} | ||
else | ||
{ | ||
writer.WriteNumberValue(value); | ||
} | ||
|
||
static bool IsNegativeZero(double x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this method is unnecessary. Based on your comment you rely on |
||
{ | ||
// Negative zero is less trivial to test, because 0 == -0 is true | ||
// We need to do a bit pattern comparison | ||
|
||
return BitConverter.DoubleToInt64Bits(x) == BitConverter.DoubleToInt64Bits(-0.0); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Pr description you mentioned:
I propose to remove it. My strong opinion: we should understand what we are doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like "we enable all serializations features supported by system.text.json, but we don't know whether it is necessary".