-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add Strict JsonSerializerOptions #116271
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 Strict JsonSerializerOptions #116271
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
102fa6b to
fdad6fc
Compare
fdad6fc to
ff6214f
Compare
|
@krwq suggested that the default value |
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.
Pull Request Overview
This PR introduces a new JsonSerializerOptions.Strict preset that disallows unmapped members, forbids duplicate properties, enforces case-sensitive binding, and respects nullable annotations and required constructor parameters. It adds support for JsonSerializerDefaults.Strict in the core API and source-gen contexts, updates singleton getters, and covers the new behavior with tests.
- Implement
JsonSerializerDefaults.Strictenum andJsonSerializerOptions.Strictproperty - Update the
JsonSerializerOptionsconstructor and default/web getters to support Strict - Add unit tests for both runtime and source-generated contexts using Strict defaults
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs | Added tests for JsonSerializerOptions.Strict behavior |
| src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSourceGenerationOptionsTests.cs | Added source-gen tests for Strict defaults |
| src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs | Added Strict property, updated default/Web getters and constructor |
| src/libraries/System.Text.Json/ref/System.Text.Json.cs | Exposed JsonSerializerDefaults.Strict and JsonSerializerOptions.Strict in reference assembly |
| src/libraries/System.Text.Json/Common/JsonSerializerDefaults.cs | Defined the Strict enum value with XML documentation |
Comments suppressed due to low confidence (1)
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs:1325
- Add a test case to verify that
JsonSerializerOptions.Strictthrows aJsonExceptionwhen an unmapped JSON property is encountered, to fully cover theJsonUnmappedMemberHandling.Disallowbehavior.
public static void JsonSerializerOptions_Strict_SerializesWithExpectedSettings()
It's too much of a breaking change. It would need to be opt in unfortunately. |
src/libraries/System.Text.Json/Common/JsonSerializerDefaults.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/Common/JsonSerializerDefaults.cs
Outdated
Show resolved
Hide resolved
|
/ba-g This code doesn't affect iOS and those tests are failing for other PR builds as well |
Add
JsonSerializerOptions.Strictwhich:JsonUnmappedMemberHandling.DisallowpolicyAllowDuplicatePropertiesRespectNullableAnnotationsandRespectRequiredConstructorParameterssettingsCloses #108526