Reconsider the semantics of JsonProperty.ShouldSerialize
null values
#71964
Labels
area-System.Text.Json
enhancement
Product code improvement that does NOT require public API changes/additions
Milestone
Originally posted by @krwq in #71908 (comment)
Under the current implementation, a null
ShouldSerialize
value will result in an ignore policy being determined by eitherJsonSerializer.DefaultIgnoreCondition
orJsonSerializer.IgnoreNullValues
settings. This is despite the fact that we are documentingnull
as meaning "always serialize". While there are merits to the current approach there are a few downsides to be considered as well:ShouldSerialize = null;
should clearly result in no serializability checks being made. This is similar to howJsonIgnoreAttribute
overridesDefaultJsonIgnoreCondition
.ShouldSerialize
checks for everyJsonSerializerOptions
configuration 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.JsonSerializerOptions
settings 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.DefaultIgnoreCondition
andIgnoreNullValues
is inconsistent with howIgnoreReadOnlyProperties
andIgnoreReadOnlyFields
are 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.
The text was updated successfully, but these errors were encountered: