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

Add JsonUtf8Reader (for ReadOnlySpan) along with unit tests#33216

Merged
ahsonkhan merged 65 commits into
dotnet:masterfrom
ahsonkhan:AddJsonUtf8Reader
Nov 9, 2018
Merged

Add JsonUtf8Reader (for ReadOnlySpan) along with unit tests#33216
ahsonkhan merged 65 commits into
dotnet:masterfrom
ahsonkhan:AddJsonUtf8Reader

Conversation

@ahsonkhan
Copy link
Copy Markdown

@ahsonkhan ahsonkhan commented Nov 2, 2018

TODO: (in future PRs, will file issues for some and replace the TODO comments in code)

image

Out-dated code coverage numbers

image

cc @joshfree, @KrzysztofCwalina, @steveharter, @GrabYourPitchforks, @davidfowl, @stephentoub

Comment thread src/System.Text.Json/ref/System.Text.Json.cs Outdated
Comment thread src/System.Text.Json/ref/System.Text.Json.cs Outdated
Comment thread src/System.Text.Json/ref/System.Text.Json.cs
Comment thread src/System.Text.Json/ref/System.Text.Json.cs
Comment thread src/System.Text.Json/ref/System.Text.Json.cs Outdated
Comment thread src/System.Text.Json/ref/System.Text.Json.cs Outdated
Comment thread src/System.Text.Json/src/Resources/Strings.resx Outdated
@ahsonkhan
Copy link
Copy Markdown
Author

@dotnet-bot test NETFX x86 Release Build

https://mc.dot.net/#/user/ahsonkhan/pr~2Fjenkins~2Fdotnet~2Fcorefx~2Fmaster~2F/test~2Ffunctional~2Fcli~2F/21f4313e654ac04b446063f0998ef56bd808187e/workItem/System.IO.Compression.Tests/analysis/xunit/System.IO.Compression.DeflateStreamUnitTests~2FWriteAsync_DuringWriteAsync

Message :
Original WriteAsync Task did not complete in time
Expected: True
Actual:   False
Stack Trace :
   at System.IO.Compression.CompressionStreamUnitTestBase.WriteAsync_DuringWriteAsync() in D:\j\workspace\windows-TGrou---2a8f9c29\src\Common\tests\System\IO\Compression\CompressionStreamUnitTestBase.cs:line 150

@ahsonkhan
Copy link
Copy Markdown
Author

Any other PR feedback?

Comment thread src/System.Text.Json/src/System/Text/Json/Utf8JsonReader.cs
@ahsonkhan
Copy link
Copy Markdown
Author

https://mc.dot.net/#/user/ahsonkhan/pr~2Fjenkins~2Fdotnet~2Fcorefx~2Fmaster~2F/test~2Ffunctional~2Fcli~2F/95bab3794b4ec70417e49787dcfaf0029bec4cc5/workItem/System.IO.Compression.Tests/analysis/xunit/System.IO.Compression.DeflateStreamUnitTests~2FWriteAsync_DuringWriteAsync

Unhandled Exception of Type Xunit.Sdk.TrueException
Message :
Original WriteAsync Task did not complete in time
Expected: True
Actual:   False
Stack Trace :
   at System.IO.Compression.CompressionStreamUnitTestBase.WriteAsync_DuringWriteAsync() in D:\j\workspace\windows-TGrou---2a8f9c29\src\Common\tests\System\IO\Compression\CompressionStreamUnitTestBase.cs:line 150

Maybe we need to increase the timeout?

Assert.True(task.Wait(10 * 500), "Original WriteAsync Task did not complete in time");

@ahsonkhan
Copy link
Copy Markdown
Author

ahsonkhan commented Nov 9, 2018

OK, So I am not sure why my recent changes would cause CI to fail (it was passing earlier):
https://mc.dot.net/#/user/ahsonkhan/pr~2Fjenkins~2Fdotnet~2Fcorefx~2Fmaster~2F/test~2Ffunctional~2Fcli~2F/95bab3794b4ec70417e49787dcfaf0029bec4cc5/workItem/System.Text.Json.Tests/analysis/xunit/System.Text.Json.Tests.Utf8JsonReaderTests~2FTestingNumbers

I am using the same seed for the random number generator. Is this an issue with doubles not round-tripping or am I missing something? The actual and expected match (at least from the output).

Message :
Assert.Equal() Failure
Expected: 29607.7425729519
Actual:   29607.7425729519
Stack Trace :
   at System.Text.Json.Tests.Utf8JsonReaderTests.TestingNumbers() in /Users/dotnet-bot/j/workspace/dotnet_corefx/master/osx-TGroup_netcoreapp+CGroup_Debug+AGroup_x64+TestOuter_false_prtest/src/System.Text.Json/tests/Utf8JsonReaderTests.TryGet.cs:line 191

@dotnet-bot test Windows x86 Release Build
@dotnet-bot test Windows x64 Debug Build

@danmoseley
Copy link
Copy Markdown
Member

Maybe it picked up @tannergooding floating point changes in the latest run?

@ahsonkhan
Copy link
Copy Markdown
Author

ahsonkhan commented Nov 9, 2018

Maybe it picked up @tannergooding floating point changes in the latest run?

Yep, that is what I am looking into now. I am going to merge master into my branch and try to repro locally. The repro tool isn't working atm.

This dotnet/coreclr#20707 came in as part of #33324

@danmoseley
Copy link
Copy Markdown
Member

Presumably the assert message is doing rounding otherwise it doesn't makes sense

@ahsonkhan
Copy link
Copy Markdown
Author

Yep, inspecting double values without strings is difficult, but looks like the actual double values are different.

Original double value: 29607.7425729519
Byte array (BitConverter.GetBytes): 224, 179, 80, 134, 239, 233, 220, 64
Expected byte array: 211, 179, 80, 134, 239, 233, 220, 64 (note the first byte is different from original)
Actual byte array: 212, 179, 80, 134, 239, 233, 220, 64 (note the first byte is different again)

// doubles[count] = 29607.7425729519
var str = string.Format(CultureInfo.InvariantCulture, "{0}", doubles[count]);
double expected = double.Parse(str, CultureInfo.InvariantCulture); // expected != doubles[count]

json.TryGetDoubleValue(out double numberDouble); // returns true

// numberDouble != doubles[count] && numberDouble != expected 
Assert.Equal(expected, numberDouble); // fails

@ahsonkhan
Copy link
Copy Markdown
Author

Hmm, looks like Utf8Parser.TryParse is giving the incorrect value. cc @tannergooding

Another one:
Expected: -4.3211173888714931E+306
Actual: -4.32111738887149E+306
Expected: 28, 7, 254, 168, 40, 157, 152, 255
Actual: 23, 7, 254, 168, 40, 157, 152, 255

Tried to use "R" and "G17" to get some round-tripped values:

string roundTripActual = numberDouble.ToString("G17", CultureInfo.InvariantCulture);
double actual = double.Parse(roundTripActual, CultureInfo.InvariantCulture);

string roundTripExpected = doubles[count].ToString("G17", CultureInfo.InvariantCulture);
double expected = double.Parse(roundTripExpected, CultureInfo.InvariantCulture);

@tannergooding
Copy link
Copy Markdown
Member

Hmm, looks like Utf8Parser.TryParse is giving the incorrect value

Yep. The Utf8Parser and Utf16Parser currently live apart and cannot easily share code. I am currently working on moving the Utf8Parser/Formatter into S.P.Corelib so that it can share all the necessary code, as needed.

  • This should get done sometime tomorrow, currently just waiting on some stuff to finish pumping before I put up the PR that fixes the Utf8Parser.

@ahsonkhan
Copy link
Copy Markdown
Author

This should get done sometime tomorrow, currently just waiting on some stuff to finish pumping before I put up the PR that fixes the Utf8Parser.

Sounds good. Thanks for the update. I added a workaround in the tests temporarily to unblock my PR.
https://github.com/dotnet/corefx/issues/33360

@ahsonkhan ahsonkhan merged commit 4375952 into dotnet:master Nov 9, 2018
@ahsonkhan ahsonkhan deleted the AddJsonUtf8Reader branch November 9, 2018 10:53
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…orefx#33216)

* Initial check-in for System.Text.Json library and solution

* Auto-generate the reference assembly

* Provide an overview README and roadmap

* Change the solution GUID to be unique

* Fix package description nit

* Change to MIT license instead of GPL

* Fix file extension of images to png

* Address PR feedback and only support netcoreapp

* Fix the configuration props and project files

* Add comments to the JsonTokenType enum

* Add an internal class for commonly used constants when reading JSON.

* Add a comment handling enum for lenient JSON reading

* Add a JsonReaderOptions struct to provide extensible customization of
JsonReader behaviour

* Add a JsonReaderException class which is thrown when we see an invalid
JSON text

* Add the skeleton for the JsonUtf8Reader ref struct and add files to
csproj

* Add JsonReaderState, ThrowHelper, and Strings resource file

* Address PR feedback, remove OS-specific configurations

* Complete the implementation of core JsonReader APIs for reading spans

* Update the reference assembly by auto-generating it

* Add unit tests and TryGet APIs

* Add XML comments to describe the JsonUtf8Reader at a high level

* Fix config and csproj to reference S.R.CS.U from Corelib

* Rename type from JsonUtf8Reader to Utf8JsonReader

* Remove remnants of ReadOnlySequeunce related fields/properties

* Add the remaining TryGetValueAsX APIs

* Change JsonReaderState ctor to accept JsonReaderOptions instead

* Seal the JsonReaderException class

* Rename LineBytePosition to BytePositionInLine

* Fix typo in ArrayEndWithinObject exception message

* Capitalize the acronym JSON within exception messages.

* Override GetObjectData and add a private deserialization ctor on
JsonReaderException

* Be explicit with lessThan in the IndexOfAny call site

* Only use ulong when Options isn't AllowComments to align stack usage

* Rename IndexOfAny helper to IndexOfOrLessThan

* Remove stray comment and fix xml comment for BytesConsumed

* Braces everywhere

* Rename localCopy to localBuffer to tie it to _buffer

* Add IsDigit and IsHexDigit helper methods

* Rename Solidus to Slash, ReverseSolidus to BackSlash, and Use Asterisk constant

* Use static encoder and avoid unnecessary allocation.

* Combine multiple TryParse calls that only differ by standardFormat

* Use SR.Format instead of string.Format and add debug.fail for default
case

* Make the exception as serializable and add test to BinaryFormatterTestData

* Fix exception messages, avoid printing control characters, and remove
unnecessary else.

* Make sure all exception messages are consistent and that they make sense
in context.

* Fix and add new comments, rename internal enum values, and remove
JsonLab

* Throw InvalidCastException from TryGetAsX if token type doesn't match

* Resolve TODOs, add issues, fix search for end of string, and remove recursive calls + add more tests.

* Use Unicode escape sequences for non-ASCII characters in tests.

* Add more tests related to skipping and consuming comments.

* Allocate the stack lazily, only when depth actually surpasses 64.

* Add a netcoreapp config to
System.Runtime.Serialization.Formatters.Tests.csproj

* Remove unnecessary DefineConstants property in test csproj

* Use Invariant Culture where necessary to avoid tests failing on es-ES
machines

* Fix up nits in the exception messages

* Add an IsTokenTypePrimitive helper method and fix comment spelling

* Use helper, use while(true) instead of goto, and change TryGet methods
to throw InvalidOperationException

* Shorten CommentHandling enum values and rename Default to Disallow

* Fix "is the stack empty" check in EndObject/EndArray

* Add previous token type to state and update it when token type is not a
comment

* Include comment delimiters as part of comment token, fix Debug.Assert
and tests.

* Add helper to avoid code duplication

* Add temporary workaround for Utf8 double parsing issue


Commit migrated from dotnet/corefx@4375952
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.