Add Utf8JsonWriter.Reset overloads accepting JsonWriterOptions#126578
Add Utf8JsonWriter.Reset overloads accepting JsonWriterOptions#126578eiriktsarpalis merged 4 commits intodotnet:mainfrom
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
There was a problem hiding this comment.
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)andUtf8JsonWriter.Reset(IBufferWriter<byte>, JsonWriterOptions)overloads (plus ref-assembly declarations). - Updated
Utf8JsonWriterdisposal tracking to use an explicit_isDisposedflag, 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. |
7c6a967 to
0b41f3a
Compare
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>
0b41f3a to
53fad00
Compare
There was a problem hiding this comment.
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
ConfigureForCacheReuseassigns_outputdirectly without a null check/guard. Even though current callers pass a non-null writer, addingArgumentNullException.ThrowIfNull(bufferWriter)(or at least aDebug.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);
}
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs
Show resolved
Hide resolved
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/ba-g unrelated CI hang. |
…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>
Note
This PR description was generated with GitHub Copilot.
Resolves #123221.
Summary
Utf8JsonWriter.Resetoverloads that acceptJsonWriterOptionsfor bothStreamandIBufferWriter<byte>outputs.Dispose,CheckNotDisposed, and internal serializer cache behavior unchanged.Testing
dotnet build src\\libraries\\System.Text.Json\\src\\System.Text.Json.csproj -c Debugdotnet 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"