Skip to content

Add Utf8JsonWriter.Reset overloads accepting JsonWriterOptions#126578

Merged
eiriktsarpalis merged 4 commits intodotnet:mainfrom
eiriktsarpalis:prototype/utf8jsonwriter-reset
Apr 7, 2026
Merged

Add Utf8JsonWriter.Reset overloads accepting JsonWriterOptions#126578
eiriktsarpalis merged 4 commits intodotnet:mainfrom
eiriktsarpalis:prototype/utf8jsonwriter-reset

Conversation

@eiriktsarpalis
Copy link
Copy Markdown
Member

@eiriktsarpalis eiriktsarpalis commented Apr 6, 2026

Note

This PR description was generated with GitHub Copilot.

Resolves #123221.

Summary

  • Adds the approved Utf8JsonWriter.Reset overloads that accept JsonWriterOptions for both Stream and IBufferWriter<byte> outputs.
  • Keeps the existing Dispose, CheckNotDisposed, and internal serializer cache behavior unchanged.
  • Adds test coverage for the new reset overloads and option-reset behavior.

Testing

  • dotnet build src\\libraries\\System.Text.Json\\src\\System.Text.Json.csproj -c Debug
  • dotnet test src\\libraries\\System.Text.Json\\tests\\System.Text.Json.Tests\\System.Text.Json.Tests.csproj -c Debug -f net11.0 --logger "console;verbosity=minimal"

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

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

This PR adds new Utf8JsonWriter.Reset overloads that accept JsonWriterOptions, enabling reuse of a writer instance while changing both the output destination and writer configuration. It also adjusts the JsonSerializer writer caching path to safely reuse cached writers even if a converter incorrectly disposes the writer during serialization.

Changes:

  • Added public Utf8JsonWriter.Reset(Stream, JsonWriterOptions) and Utf8JsonWriter.Reset(IBufferWriter<byte>, JsonWriterOptions) overloads (plus ref-assembly declarations).
  • Updated Utf8JsonWriter disposal tracking to use an explicit _isDisposed flag, and introduced a thread-local “cache reuse in progress” flag to avoid cache poisoning scenarios.
  • Added targeted regression tests covering invalid argument behavior, option changes via reset, output-mode switches with new options, and the JsonSerializer cache scenario involving a disposed writer.

Reviewed changes

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

File Description
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonWriterTests.cs Adds coverage for the new Reset overloads and a regression test ensuring serializer caching isn’t broken by a converter disposing the writer.
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriterCache.cs Wraps cached-writer reset in a thread-local reuse flag so cached instances can be safely reset even if previously disposed unexpectedly.
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs Implements the new public Reset overloads and revises disposed-state handling to support safe cache reuse.
src/libraries/System.Text.Json/ref/System.Text.Json.cs Updates the reference assembly to include the new public Reset overloads.

@eiriktsarpalis eiriktsarpalis force-pushed the prototype/utf8jsonwriter-reset branch from 7c6a967 to 0b41f3a Compare April 6, 2026 16:39
Add two new public Reset overloads to Utf8JsonWriter:
- Reset(IBufferWriter<byte> bufferWriter, JsonWriterOptions options)
- Reset(Stream utf8Json, JsonWriterOptions options)

These enable pooling scenarios where both the output destination
and writer options need to change between reuses.

The existing internal Reset(IBufferWriter, JsonWriterOptions) method
used by Utf8JsonWriterCache is renamed to ConfigureForCacheReuse to
avoid signature collision with the new public API.

Prototype for: dotnet#123221

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@eiriktsarpalis eiriktsarpalis force-pushed the prototype/utf8jsonwriter-reset branch from 0b41f3a to 53fad00 Compare April 6, 2026 16:55
Copilot AI review requested due to automatic review settings April 6, 2026 16:55
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 4 out of 4 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs:428

  • ConfigureForCacheReuse assigns _output directly without a null check/guard. Even though current callers pass a non-null writer, adding ArgumentNullException.ThrowIfNull(bufferWriter) (or at least a Debug.Assert(bufferWriter is not null)) would make the method more robust against accidental misuse and fail fast with a clearer error.
        internal void ConfigureForCacheReuse(IBufferWriter<byte> bufferWriter, JsonWriterOptions options)
        {
            Debug.Assert(_output is null && _stream is null && _arrayBufferWriter is null);

            _output = bufferWriter;
            SetOptions(options);
        }

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

LGTM!

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 6, 2026 17:24
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 4 out of 4 changed files in this pull request and generated 1 comment.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@eiriktsarpalis
Copy link
Copy Markdown
Member Author

/ba-g unrelated CI hang.

@eiriktsarpalis eiriktsarpalis merged commit 93578ff into dotnet:main Apr 7, 2026
21 of 24 checks passed
@eiriktsarpalis eiriktsarpalis deleted the prototype/utf8jsonwriter-reset branch April 7, 2026 10:23
radekdoulik pushed a commit to radekdoulik/runtime that referenced this pull request Apr 9, 2026
…t#126578)

> [!NOTE]
> This PR description was generated with GitHub Copilot.

Resolves dotnet#123221.

## Summary
- Adds the approved `Utf8JsonWriter.Reset` overloads that accept
`JsonWriterOptions` for both `Stream` and `IBufferWriter<byte>` outputs.
- Keeps the existing `Dispose`, `CheckNotDisposed`, and internal
serializer cache behavior unchanged.
- Adds test coverage for the new reset overloads and option-reset
behavior.

## Testing
- `dotnet build
src\\libraries\\System.Text.Json\\src\\System.Text.Json.csproj -c Debug`
- `dotnet test
src\\libraries\\System.Text.Json\\tests\\System.Text.Json.Tests\\System.Text.Json.Tests.csproj
-c Debug -f net11.0 --logger "console;verbosity=minimal"`

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Add Utf8JsonWriter.Reset overloads accepting new JsonWriterOptions configuration

4 participants