-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add ReadOnlySpan<char> overloads to JsonSerializer.Deserialize #41957
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
Conversation
|
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to 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. |
cc @safern on generic type parameters not being supported by Opened #42186 to re-add linker attributes omitted from generic type params on @huoyaoyuan what other pain points do you have with |
The way to test it is to add a new runtime/src/libraries/System.Text.Json/tests/Serialization/DeserializationWrapper.cs Lines 23 to 34 in d1f48a2
Use the new wrapper wherever the existing ones are used. We also need to add linker trimming tests for the new overloads. See the exisiting ones here. Each overload should map to a new
|
layomia
left a comment
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 for the PR. I just have a few comments and a pointer on the tests.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs
Show resolved
Hide resolved
We have an issue for APICompat, since it is assigned to me I will look at GenAPI as well dotnet/arcade#5925 |
| /// <typeparamref name="TValue"/> is not compatible with the JSON, | ||
| /// or when there is remaining data in the Stream. |
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.
This overload doesn't take a stream. Doc comment needs to be updated.
I recommend using the content from the official msft docs that have gone through content review to seed the new API docs rather than copying from an overload to avoid slight discrepancies.
https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer.deserialize?view=netcore-3.1#System_Text_Json_JsonSerializer_Deserialize__1_System_String_System_Text_Json_JsonSerializerOptions_
| /// the <paramref name="returnType"/> is not compatible with the JSON, | ||
| /// or when there is remaining data in the Stream. |
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.
layomia
left a comment
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.
Suggested doc changes for consistency with what is in https://docs.microsoft.com/dotnet/api/system.text.json.jsonserializer.deserialize?view=netcore-3.1.
Linker trimming tests as described in #41957 (comment) are needed to merge this PR.
| { | ||
| if (json == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(json)); |
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.
We shouldn't throw ArgumentNullException for a value type. Instead we should let the reader throw a JsonException similar to the behavior for an empty ReadOnlySpan<byte>:
JsonSerializer.Deserialize(default(ReadOnlySpan<byte>), typeof(int));
Unhandled exception. System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
This applies to the other new overload as well. We should add a test for this scenario for both overloads and assert on the content of the exception. The path information should be present in the exception string "$ | LineNumber: 0 | BytePositionInLine: 0".
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.
This Comment suggests a special test for default span. Otherwise, default span will be treated same as empty span.
What's the final decision on this?
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.
Otherwise, default span will be treated same as empty span.
Default and empty spans should be treated the same. Adding a test passing in default span is good to have, but the null check within the source implementation isn't necessary and should be removed.
In the implementation of Deserialize, a default/empty span should be allowed to be passed to the Utf8JsonReader, and it will validate that empty JSON is invalid and throw JsonException. That is the desired behavior.
|
|
||
| if (returnType == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(returnType)); |
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.
Need test exercising this line.
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.
Also, need to add to the doc comment that this method can throw ArgumentNullException in the list of Exceptions.
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.
Similar to this:
https://github.com/dotnet/dotnet-api-docs/blob/fcf78e35bc4eca5734d3f248670b1ade805e2ed0/xml/System.Text.Json/JsonSerializer.xml#L75-L76
<exception cref="T:System.ArgumentNullException">
<paramref name="returnType" /> is <see langword="null" />.</exception>
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Layomi Akinrinade <layomia@gmail.com>
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.String.cs
Outdated
Show resolved
Hide resolved
ahsonkhan
left a comment
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.
Some docs feedback. Looks good otherwise.
| /// There is remaining data in the span beyond a single JSON value.</exception> | ||
| /// <exception cref="NotSupportedException"> | ||
| /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> | ||
| /// for <typeparamref name="TValue"/> or its serializable members. |
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.
@layomia, @tdykstra, can we please add or its serializable members to the docs to all such methods, in the published api-docs?
It doesn't show that currently. For example:
https://docs.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer.deserialize?view=netcore-3.1#System_Text_Json_JsonSerializer_Deserialize_System_String_System_Type_System_Text_Json_JsonSerializerOptions_
Co-authored-by: Ahson Khan <ahkha@microsoft.com>
|
@layomia - anything else left to do for this PR or is it ready for merging? |
layomia
left a comment
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.
LGTM - will address linker tests for the new overload in a follow up.
Closes #1235 .
I'm not sure how to test it. The existing tests are focused on data instead of overloads.
Contribution pain: I have never been satisfied by
build /t:GenerateReferenceSourcethese days, for different reasons. For System.Text.Json, the reason is that attribute on generic parameter is not supported.