-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additionsProduct code improvement that does NOT require public API changes/additions
Milestone
Description
I'd expect
ShouldSerialize = nullbrings me back default behavior from options and not be equivalent toalways serialize.
Originally posted by @krwq in #71908 (comment)
Under the current implementation, a null ShouldSerialize value will result in an ignore policy being determined by either JsonSerializer.DefaultIgnoreCondition or JsonSerializer.IgnoreNullValues settings. This is despite the fact that we are documenting null as meaning "always serialize". While there are merits to the current approach there are a few downsides to be considered as well:
- Type-specific configuration should override global configuration. As a user, explicitly setting
ShouldSerialize = null;should clearly result in no serializability checks being made. This is similar to howJsonIgnoreAttributeoverridesDefaultJsonIgnoreCondition. - As a custom contract resolver author, it is impossible to completely disable
ShouldSerializechecks for everyJsonSerializerOptionsconfiguration unless I explicitly set a predicate that always returns true. Forcing an additional delegate on an otherwise very common usage scenario is counterintuitive and less efficient. - While a contract resolver does consult
JsonSerializerOptionssettings when doing its work, the resultant contract is no longer the single source of truth -- the final contract will instead depend on configuration that is non-modifiable by the resolver author. - The current handling of
DefaultIgnoreConditionandIgnoreNullValuesis inconsistent with howIgnoreReadOnlyPropertiesandIgnoreReadOnlyFieldsare treated. The latter group feeds into the contract model and is user-modifiable.
#71908 attempted to change this but it got reverted due to pushback. Nevertheless, I still think we should consider this.
Metadata
Metadata
Assignees
Labels
area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additionsProduct code improvement that does NOT require public API changes/additions