Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON: Add support for {ReadOnly}Memory<T> #88713

Merged
merged 4 commits into from
Jul 13, 2023

Conversation

jozkee
Copy link
Member

@jozkee jozkee commented Jul 12, 2023

Fixes #86442.

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost ghost assigned jozkee Jul 12, 2023
@ghost
Copy link

ghost commented Jul 12, 2023

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

Issue Details

Fixes #86442.

Author: Jozkee
Assignees: -
Labels:

area-System.Text.Json, new-api-needs-documentation

Milestone: -

@jozkee jozkee added this to the 8.0.0 milestone Jul 12, 2023
eiriktsarpalis
eiriktsarpalis previously approved these changes Jul 12, 2023
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.

Other than a few minor issues this looks great 👍

@eiriktsarpalis eiriktsarpalis dismissed their stale review July 13, 2023 09:17

Changes required

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.

Thanks!

@jozkee jozkee merged commit 4d5d2dc into dotnet:main Jul 13, 2023
@jozkee jozkee deleted the json-memory-converter branch July 13, 2023 15:01
Comment on lines +30 to +64
int index = state.Current.EnumeratorIndex;

JsonConverter<T> elementConverter = GetElementConverter(ref state);
ReadOnlySpan<T> valueSpan = value.Span;

if (elementConverter.CanUseDirectReadOrWrite && state.Current.NumberHandling == null)
{
// Fast path that avoids validation and extra indirection.
for (; index < valueSpan.Length; index++)
{
elementConverter.Write(writer, valueSpan[index], options);
}
}
else
{
for (; index < value.Length; index++)
{
T element = valueSpan[index];
if (!elementConverter.TryWrite(writer, element, options, ref state))
{
state.Current.EnumeratorIndex = index;
return false;
}

state.Current.EndCollectionElement();

if (ShouldFlush(writer, ref state))
{
state.Current.EnumeratorIndex = ++index;
return false;
}
}
}

return true;
Copy link
Member

Choose a reason for hiding this comment

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

Rather than duplicating all of the code for this for Memory vs ReadOnlyMemory, could there just be a shared method they both call into?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds possible, @jozkee perhaps something to follow up on in RC1?

@ghost ghost locked as resolved and limited conversation to collaborators Aug 16, 2023
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.

Feature request: treat Memory/ReadOnlyMemory<T> as T[] in System.Text.Json
3 participants