-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add ctor to JsonSerializerOptions that takes serializer defaults #36073
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
Add ctor to JsonSerializerOptions that takes serializer defaults #36073
Conversation
5e3b9d7 to
3d042df
Compare
|
skipped docs/coding-guidelines/updating-ref-source.md: ref-source was updated manually |
stephentoub
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 working on this.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerDefaults.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerDefaults.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerDefaults.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Show resolved
Hide resolved
|
Pinging ASP.NET folks and @terrajobst as there was some discussion about whether @pranavkm previously mentioned that |
| if (defaults == JsonSerializerDefaults.Web) | ||
| { | ||
| _propertyNameCaseInsensitive = true; | ||
| _jsonPropertyNamingPolicy = JsonNamingPolicy.CamelCase; |
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 - asked me to drop by and confirm whether this matches ASP.NET's expectations. The relevant code is here for anyone who wants to compare.
Some items worth of discussion:
MaxDepth- relaxed encoder
For MaxDepth - we did not scientifically compute the number for maxdepth. We have just always had a value of 32 - and yours is 64. I don't foresee a problem with us using 64. So I think we can agree to use your default of 64.
For the encoder - (this is the fun one) we do some pretty subtle stuff. Our main options class doesn't specify an encoder, and we special case it in two places:
- For writing output as JSON response from the server we opt in the relaxed encoder
- For writing JSON in HTML we force the default (strict) encoder
If we want to be technical - JSON as a document/request/response with the content type application/json is always unicode. The JSON specs all agree, that JSON is all unicode all of the time. We use the relaxed encoder when sending JSON over the wire as its own self-contained payload because all consumers should expect it to the unicode.
We don't use the relaxed encoder when JSON is being embedded inside an HTML response, because that's browser-land, and it's much more of a wierd place.
I think regardless of which encoder is used as the default we'll keep our behavior the same. I won't try to cosplay as a security expert. My assumption is that the common task using the "web" settings is to read and write JSON documents for communication with a REST API, so my intuition is that using the relaxed encoding is right.
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.
My assumption is that the common task using the "web" settings is to read and write JSON documents for communication with a REST API, so my intuition is that using the relaxed encoding is right.
I agree with you on that. Although, just Web option could mislead code clients to potential vulnerability issues. We could provide a more verbose name that communicates usage and intentions.
What do you think about adding separate enum? I this case, developer hits dot in IDE and sees that there is another thingy for HTML
- General
- Web (relaxed encoding)
- WebHtmlClient (default encoding)
Waiting for 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.
I discussed with @terrajobst - since specifying the unsafe relaxed encoder isn't recommended in some scenarios, we shouldn't specify it in a generic "web" default. Developers should specify it atop the default, depending on the scenario, as is done in an example @rynowak provided above. So, the current implementation works fine.
We don't want to have various "web" default variants (WebRest, WebHtml etc) as this would likely introduce the overhead of figuring out what the differences are, particularly if the set of defaults grows.
We'll leave max depth (effective) as 64, since we don't foresee issues with ASP.NET switching to this number.
7329138 to
65c0c05
Compare
|
@layomia. Could you please clarify the final decision regarding the Encoder? |
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.
Just a couple comments, otherwise LGTM. We can merge when addressed & conflicts are fixed.
src/libraries/System.Text.Json/tests/Serialization/OptionsTests.cs
Outdated
Show resolved
Hide resolved
…erializerdefaults-for-json-serializer-options
|
@layomia I assume you want me to add/update .NET API Reference Docs. Also, I would like to add a little section to docs/standard/serialization/system-text-json-how-to.md, something like "Use predefined JsonSerializerOptions", but I'm not sure if it is valuable enough. |
|
@layomia |
|
@NikiforovAll, I didn't intend for you to update the docs. We'll handle that e.g. with dotnet/dotnet-api-docs#4284. I was assigning authors to PRs as part of triage. Thanks for reaching out, and sorry for the noise. |
Fixes #34626
I decided to take over because of no activity of the current assignee. Let me know if I'm moving in the right direction.