-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add Utf8JsonWriter.WriteRawValue(System.Buffers.ReadOnlySequence). #76444
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
Add Utf8JsonWriter.WriteRawValue(System.Buffers.ReadOnlySequence). #76444
Conversation
|
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsFix #68223
|
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonWriterTests.WriteRaw.cs
Outdated
Show resolved
Hide resolved
This comment was marked as off-topic.
This comment was marked as off-topic.
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.
I know this check has been taken from the ReadOnlySpan overload, but I struggle to understand what it means. Why only check for int.MaxValue lengths specifically?
For the case of ReadOnlySpan, it will almost always be false (as managed buffers are bounded by Array.MaxLength which in turn is less than int.MaxValue). For the case of ReadOnlySequence the Length property is long so any value > int.MaxValue will happily pass this check.
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.
cc @layomia
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.
Fixed.
For ReadOnlySequence overload side, it is my oversight when I thought they are similar codes at first sight, sorry about that ...
For ReadOnlySpan overload side, I guess this int length cannot be int.MaxValue because subsequent code may add ','.
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.
I guess I'm wondering if the check in the ReadOnlySpan overload is correct and whether we should be fixing it.
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.
Ok, just confirm, we will see @layomia 's reply about that check.
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.
I know this check has been taken from the ReadOnlySpan overload
It wasn't.
Update the assertion in the core methods to say len <= int.MaxValue.
I recommend we leave as-is. We require the the input length is than int.MaxValue so we can add a comma if required. The comment talks about validation in the Utf16 based entry points (the info about integer division) but should also state that the ROSpan/Sequence entry points need to adhere to this to.
The alternative would be to throw only if the comma is required but I think the current logic is cleaner.
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.
Add unit tests covering these boundary conditions
I believe these are covered here but it's still worth some thought
runtime/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonWriterTests.WriteRaw.cs
Line 478 in af86947
| public static void WriteRawUtf16LengthGreaterThanMax(int len) |
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.
For new ROSeq, there was also test case added to cover boundary conditions.
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 wasn't.
I recommend we leave as-is. We require the the input length is thanint.MaxValueso we can add a comma if required. The comment talks about validation in the Utf16 based entry points (the info about integer division) but should also state that the ROSpan/Sequence entry points need to adhere to this to.
I'm not sure I follow. Even you are able to add one comma at the end, that isn't necessary to produce a complete JSON document.
My concern is that in 99.9% of use cases, this check isn't particularly meaningful since both the source and Utf8JsonWriter destination buffers are bounded by Array.MaxLength which is substantially less than int.MaxValue. You could, in theory, construct an unmanaged span of length int.MaxValue, but if you do that then your code probably has bigger problems than not being able to append one character at the end.
In any case, I probably agree we should not try to change this in the context of this PR.
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.
Utf8JsonWriter destination buffers are bounded by Array.MaxLength which is substantially less than int.MaxValue.
Yeah that's right - context I didn't keep in mind when thinking about this.
we should not try to change this in the context of this PR.
👍 yeah fine to address in a follow up.
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonWriterTests.WriteRaw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
|
This pull request has been automatically marked |
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Raw.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonWriterTests.WriteRaw.cs
Outdated
Show resolved
Hide resolved
a8159b6 to
0fe9e95
Compare
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.
LGTM, thanks! I've rebased your changes on top of the latest main, will merge once CI completes.
0fe9e95 to
2d3d4a5
Compare
|
Thank you for your contribution @lateapexearlyspeed! |
Fix #68223