Proposal: Update JsonSerializer.Deserializer (and friends) so the return type is not automatically nullable #115607
Replies: 2 comments 9 replies
-
No, it's not available. Reference nullability at non-signature are totally erased. |
Beta Was this translation helpful? Give feedback.
-
That's true, but it would hardly be the only scenario that the APIs allow through. The current limitations of RespectNullableAnnotations comes to mind, where an absent JSON property will result in the property having a null value despite the compiler telling you it won't. Applying the same logic (and being unnecessarily facetious), it technically should be forcing all reference properties on the target type to be nullable since any of those could also end up being null.
Honestly, this more than anything probably makes any other discussion irrelevant. It sounds like it just not a change that can happen unless the team created a completely separate JSON deserialization API. I appreciate you humouring me! |
Beta Was this translation helpful? Give feedback.
-
While I appreciate that there are at least two parts to this, specifically "whether the team agrees with the idea" and "how feasible it is to change in terms of public API changes", this discussion is intended to be on the former.
The
JsonSerializer.Deserialize<T>()
family of methods all returnT?
rather thanT
. This necessitates adding additional ceremony (either!
or a throw) in realistically at least 99% percent of call sites because the behaviour of the target API is usually well defined, if not owned by the same party.As far as I understand, the justification/argument for the API in its present state is that the JSON being deserialized could simply be the value
null
(being valid JSON)Assuming my understanding is correct, I'd like to offer two counterarguments:
null
, it could be another object shape, a JSON-encoded string, or a number. All are valid JSON and none are currently protected against via the deserialization method signatures.Beta Was this translation helpful? Give feedback.
All reactions