Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Scientific notation numbers with decimal separators support#39406

Merged
bartonjs merged 4 commits into
dotnet:masterfrom
vicrdguez:master
Jul 15, 2019
Merged

Scientific notation numbers with decimal separators support#39406
bartonjs merged 4 commits into
dotnet:masterfrom
vicrdguez:master

Conversation

@vicrdguez
Copy link
Copy Markdown
Contributor

@vicrdguez vicrdguez commented Jul 11, 2019

Writing Scientific notation numbers with decimal separators and exponent throwed an exception.
The decimal separator and the exponent separator were condition exclusive in the number validation

Fixes #39139.

Víctor Reyes Rodríguez added 2 commits July 3, 2019 20:49
@dnfclas
Copy link
Copy Markdown

dnfclas commented Jul 11, 2019

CLA assistant check
All CLA requirements met.

Comment thread src/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs Outdated
doubles[7] = 1e200;
doubles[8] = -1e200;
doubles[9] = 1e-200;
doubles[10] = -1e-200;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While these may be reasonable tests, these aren't testing the code change.

To test the code change you'd need to add a test like

[Theory]
[InlineData("6.022e-23", false)]
[InlineData("6.022e-23", true)]
[InlineData("6.022e23", false)]
[InlineData("6.022e23", true)]
[InlineData("6.022e+23", false)]
[InlineData("6.022e+23", true)]
[InlineData("-6.022e-23", false)]
[InlineData("-6.022e-23", true)]
[InlineData("-6.022e23", false)]
[InlineData("-6.022e23", true)]
[InlineData("-6.022e+23", false)]
[InlineData("-6.022e+23", true)]
public static void WriteNumberDecimalScientific(string value, bool indented)
{
    WriteSimpleValue(indented, value);
}

In JsonElementWriteTests.cs, around line 39 (between WriteNumberScientific and WriteNumberOverprecise).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roger that I didn't get it right

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these extra inputs, as this is an unrelated change. (These numbers were parsed by the compiler, not the JSON writer, they aren't evaluated, just written down via Utf8Formatter... because they can't be "invalid")

return;
}

//The non digit character inside the number
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, space after //

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vicrdguez Our comment style is to have a space after the // and before the words. If you feel like this comment adds value, please insert the space.

Comment thread src/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs Outdated
throw new ArgumentException(
SR.RequiredDigitNotFoundEndOfData,
nameof(utf8FormattedNumber));
throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the tests hit every one of these throws?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently hitting the throws requires either a bug in this method (how we got here 😄) or a JsonElement having gotten into an invalid/corrupted state. (This method is here to guard against that corruption and to serve as validation for a future addition to the writer where it allows you to write the numeric string directly)

Having written both this method and JsonElement, it's probably most efficient for me to write the tests to get the coverage on these throws... after we get this PR in so we get the broken success scenario fixed.

Copy link
Copy Markdown
Contributor Author

@vicrdguez vicrdguez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented fixed are done and added more test cases not only for this change, but for more cases

Comment thread src/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs Outdated
Comment thread src/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs Outdated
// because it doesn't need to deal with "NeedsMoreData", or remembering the format.
if (utf8FormattedNumber.IsEmpty)
{
//ThrowHelper.ThrowArgumentException(nameof(utf8FormattedNumber), utf8FormattedNumber);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add commented out code. If there's a ThrowHelper for this already, use it. If not, just delete the comment and be a simple throw.

[InlineData("1e400", true)]
[InlineData("-1e400", false)]
[InlineData("-1e400", true)]
public static void WriteNumberTooLargeScientific(string value, bool indented)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert the changes to this particular test. The formatting variation is already covered by the above two tests, and these changes have made the comments explaining the value of this test case be awkwardly not adjacent to the value that they're explaining. Also, now there are values instead of a single value.

}
}


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the second of the two blank lines. (Or, if you prefer, the first... not like I can tell :))

@ericstj
Copy link
Copy Markdown
Member

ericstj commented Jul 15, 2019

Fixes #39139

@bartonjs
Copy link
Copy Markdown
Member

The JSON tests have already reported in on the Windows x86 Release run (no failures, but 5 other library answers haven't posted yet), and this change is likely going to cause another merge conflict for another JSON change outstanding; so using my judgment to go ahead and merge.

@bartonjs bartonjs merged commit e88ed00 into dotnet:master Jul 15, 2019
@karelz karelz added this to the 3.0 milestone Jul 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonElement.WriteAsValue does not support scientific notation with decimal separator

6 participants