Skip to content

Fix scoped Utf8JsonReader to carry original position#127679

Merged
eiriktsarpalis merged 5 commits intodotnet:mainfrom
prozolic:getreaderscopedtonextvalue
May 6, 2026
Merged

Fix scoped Utf8JsonReader to carry original position#127679
eiriktsarpalis merged 5 commits intodotnet:mainfrom
prozolic:getreaderscopedtonextvalue

Conversation

@prozolic
Copy link
Copy Markdown
Contributor

@prozolic prozolic commented May 2, 2026

#97893

This PR capture the original reader's _lineNumber and _bytePositionInLine in JsonSerializer.GetReaderScopedToNextValue, rewind them per token type to point immediately before the value token, and pass them to the scoped reader through JsonReaderState. The scoped reader, after consuming its first token, lands on the same position as the original reader, so JsonException now reports positions relative to the original input.

In JsonSerializer.GetReaderScopedToNextValue, capture the original
reader's _lineNumber and _bytePositionInLine, rewind them per token type
to point immediately before the value token, and pass them to the scoped
reader through JsonReaderState. The scoped reader, after consuming its
first token, lands on the same position as the original reader, so
JsonException now reports positions relative to the original input.
Copilot AI review requested due to automatic review settings May 2, 2026 06:18
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 2, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

@prozolic prozolic marked this pull request as ready for review May 2, 2026 06:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR aims to preserve accurate JsonException line and byte-position information when JsonSerializer.Deserialize is called with an existing Utf8JsonReader positioned on or near a value.

Changes:

  • Capture and adjust the caller reader's line/byte position before creating a scoped reader for the next value.
  • Initialize the scoped Utf8JsonReader with preserved position metadata instead of starting from a fresh state.
  • Add regression tests for position reporting across primitive, property-name, none-token, and multi-segment reader scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ReadValueTests.cs Adds regression tests covering preserved exception position info for several reader states.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Utf8JsonReader.cs Updates scoped-reader creation to carry forward line/byte position and rewind it to the start of the current value.

Copilot AI review requested due to automatic review settings May 5, 2026 00:56
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Utf8JsonReader.cs:1

  • The new segmented-string path is not exercised by the added tests. ReaderPreservesPositionInfoMultiSegment covers numbers, booleans, and containers, but not string tokens, so regressions in payloadLength or byte-position calculations for multi-segment strings would go unnoticed. Add at least one multi-segment string case here, ideally with an escaped string as well since that uses the same offset math.
// Licensed to the .NET Foundation under one or more agreements.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 5, 2026 01:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Utf8JsonReader.cs:1

  • The new rewind logic for string tokens is only covered by plain strings in the added tests. Escaped strings (\\uXXXX, \\\", etc.) use the same raw-byte length math but have different on-the-wire sizes, so a regression here would not be caught. Please add a case that starts from an escaped string token and verifies the reported line and byte position.
// Licensed to the .NET Foundation under one or more agreements.

Copy link
Copy Markdown
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.

Thanks

@eiriktsarpalis
Copy link
Copy Markdown
Member

/ba-g test failures are unrelated.

@eiriktsarpalis eiriktsarpalis enabled auto-merge (squash) May 6, 2026 11:46
@eiriktsarpalis eiriktsarpalis merged commit 055b99b into dotnet:main May 6, 2026
84 of 89 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Text.Json community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants