-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Conversation
Note regarding the 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. |
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsFixes #86442.
|
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Memory.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Memory.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonTestHelper.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
Fixes #86442.