Skip to content
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

lateapexearlyspeed
Copy link
Contributor

Fix #68223

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

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.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Sep 30, 2022
@ghost
Copy link

ghost commented Sep 30, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Fix #68223

Author: lateapexearlyspeed
Assignees: -
Labels:

area-System.Text.Json, new-api-needs-documentation

Milestone: -

@Wanath

This comment was marked as off-topic.

ValidateWritingValue();
}

if (utf8Json.Length == int.MaxValue)
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

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 ','.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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

public static void WriteRawUtf16LengthGreaterThanMax(int len)

Copy link
Contributor Author

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.

Copy link
Member

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 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.

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.

Copy link
Contributor

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.

@jeffhandley jeffhandley added the needs-author-action An issue or pull request that requires more info or actions from the author. label Dec 2, 2022
@ghost ghost added the no-recent-activity label Dec 16, 2022
@ghost
Copy link

ghost commented Dec 16, 2022

This pull request has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.

@ghost ghost removed no-recent-activity needs-author-action An issue or pull request that requires more info or actions from the author. labels Dec 16, 2022
@build-analysis build-analysis bot mentioned this pull request Dec 25, 2022
@eiriktsarpalis eiriktsarpalis force-pushed the lateapexearlyspeed-WriteRawValue(ReadOnlySequence) branch from a8159b6 to 0fe9e95 Compare January 10, 2023 11:00
Copy link
Member

@eiriktsarpalis eiriktsarpalis left a 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.

@eiriktsarpalis eiriktsarpalis force-pushed the lateapexearlyspeed-WriteRawValue(ReadOnlySequence) branch from 0fe9e95 to 2d3d4a5 Compare January 25, 2023 13:46
@eiriktsarpalis
Copy link
Member

Thank you for your contribution @lateapexearlyspeed!

@eiriktsarpalis eiriktsarpalis merged commit 30cc26f into dotnet:main Jan 25, 2023
@eiriktsarpalis eiriktsarpalis added this to the 8.0.0 milestone Jan 31, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Mar 2, 2023
@jeffhandley jeffhandley added the blog-candidate Completed PRs that are candidate topics for blog post coverage label Mar 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json blog-candidate Completed PRs that are candidate topics for blog post coverage community-contribution Indicates that the PR has been added by a community member new-api-needs-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: Add ReadOnlySequence overload for WriteRawValue
6 participants