Skip to content

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

Merged

Conversation

PranavSenthilnathan
Copy link
Member

@PranavSenthilnathan PranavSenthilnathan commented Jun 3, 2025

Add JsonSerializerOptions.Strict which:

  • Applies the JsonUnmappedMemberHandling.Disallow policy
  • Disables AllowDuplicateProperties
  • Preserves case sensitive property binding
  • Enables both RespectNullableAnnotations and RespectRequiredConstructorParameters settings

Closes #108526

Copy link
Contributor

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

@PranavSenthilnathan
Copy link
Member Author

@krwq suggested that the default value AllowDuplicateProperties (even without JsonSerializerOptions.Strict) should be false since that's a safer default. This would be a breaking change, so I'm not sure if we can do it. @eiriktsarpalis thoughts?

@PranavSenthilnathan PranavSenthilnathan marked this pull request as ready for review June 10, 2025 17:59
@Copilot Copilot AI review requested due to automatic review settings June 10, 2025 17:59
Copy link
Contributor

@Copilot Copilot AI left a 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.Strict enum and JsonSerializerOptions.Strict property
  • Update the JsonSerializerOptions constructor 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.Strict throws a JsonException when an unmapped JSON property is encountered, to fully cover the JsonUnmappedMemberHandling.Disallow behavior.
public static void JsonSerializerOptions_Strict_SerializesWithExpectedSettings()

@eiriktsarpalis
Copy link
Member

This would be a breaking change, so I'm not sure if we can do it. @eiriktsarpalis thoughts?

It's too much of a breaking change. It would need to be opt in unfortunately.

@PranavSenthilnathan
Copy link
Member Author

/ba-g This code doesn't affect iOS and those tests are failing for other PR builds as well

@PranavSenthilnathan PranavSenthilnathan added this to the 10.0.0 milestone Jun 11, 2025
@PranavSenthilnathan PranavSenthilnathan merged commit f1bff2a into dotnet:main Jun 11, 2025
84 of 86 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose a new JsonSerializerDefaults providing stricter defaults for cloud applications.
2 participants