Fix scoped Utf8JsonReader to carry original position#127679
Fix scoped Utf8JsonReader to carry original position#127679eiriktsarpalis merged 5 commits intodotnet:mainfrom
Conversation
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.
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
There was a problem hiding this comment.
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
Utf8JsonReaderwith 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. |
There was a problem hiding this comment.
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.
ReaderPreservesPositionInfoMultiSegmentcovers numbers, booleans, and containers, but not string tokens, so regressions inpayloadLengthor 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>
There was a problem hiding this comment.
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.
|
/ba-g test failures are unrelated. |
#97893
This PR capture the original reader's
_lineNumberand_bytePositionInLineinJsonSerializer.GetReaderScopedToNextValue, rewind them per token type to point immediately before the value token, and pass them to the scoped reader throughJsonReaderState. The scoped reader, after consuming its first token, lands on the same position as the original reader, soJsonExceptionnow reports positions relative to the original input.