Skip to content

Conversation

@BrennanConroy
Copy link
Member

Closes #68586

Didn't add any new tests for multi-segment deserialize as there is decent coverage in https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.MultiSegment.cs already and this PR doesn't add much on top of the Utf8JsonReader layer. And we should be using fuzzing (separate PR after this one) to get more randomized segment size coverage.

The DeserializeAsyncEnumerable APIs technically weren't part of the approved API, but they're just a copy of the Stream ones so should probably be fine.

Added IReadBufferState<TReadBufferState> to abstract processing PipeReader and Stream.

Updated the test Pipe serializer to use the new APIs and added it to a bunch of the base tests.

@eiriktsarpalis
Copy link
Member

And we should be using fuzzing (separate PR after this one) to get more randomized segment size coverage.

Given we're very close to GA we should be doing this sooner rather than later.

public readonly bool IsFinalBlock => _isFinalBlock;

public readonly ReadOnlySpan<byte> Bytes => _buffer.AsSpan(_offset, _count);
public readonly ReadOnlySequence<byte> Bytes => new(_buffer.AsMemory(_offset, _count));
Copy link
Member

@eiriktsarpalis eiriktsarpalis Jul 18, 2025

Choose a reason for hiding this comment

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

I take it we're switching to sequence-based Utf8JsonReaders for streams with this change? Given we're close to RC, I'm slightly concerned that any issues with that implementation would surface as regressions in the stream-based APIs. Is there any chance we could keep this implementation in span mode? One way to do that is delegate Utf8JsonReader construction to a IReadBufferState method.

Copy link
Member Author

Choose a reason for hiding this comment

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

We look at whether the ReadOnlySequence is a single segment and use the Span overload in that case. Stream will always be a single segment so there won't be any regressions there.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, but even so the instance is constructed using the multi-segnent modality so different code paths are being entered.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is still constructing the Utf8JsonReader with the ReadOnlySpan overload
image

The only difference is that we first create a ReadOnlySequence https://source.dot.net/#System.Memory/System/Buffers/ReadOnlySequence.cs,113

We can change IReadBufferState to have a construction method like you mentioned, but it doesn't change what is happening.

Copy link
Member

Choose a reason for hiding this comment

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

If you feel confident that it won't cause any regressions, go for it. I just want us to be extra cautious given we're so close to RC.

@BrennanConroy
Copy link
Member Author

And we should be using fuzzing (separate PR after this one) to get more randomized segment size coverage.

Given we're very close to GA we should be doing this sooner rather than later.

Do you want a PR opened for it that targets this branch?

@eiriktsarpalis
Copy link
Member

Do you want a PR opened for it that targets this branch?

I don't think we need to, but any validation must be run before RC snap.

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.

I think we're past the snap deadline, so this will probably need to be backported to the p7 branch. @tarekgh can you confirm?

@BrennanConroy
Copy link
Member Author

/ba-g timeouts are known issues with azurelinux queue, test failures are also known test failures

@BrennanConroy BrennanConroy merged commit f175036 into dotnet:main Jul 21, 2025
80 of 87 checks passed
@BrennanConroy BrennanConroy deleted the brecon/jsonpipereader branch July 21, 2025 23:18
@BrennanConroy
Copy link
Member Author

/backport to release/10.0-preview7

@github-actions
Copy link
Contributor

Started backporting to release/10.0-preview7: https://github.com/dotnet/runtime/actions/runs/16430376722

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Add PipeReader and PipeWriter overloads to JsonSerializer.SerializeAsync/DeserializeAsync

3 participants