How should we handle JsonSerializer.DeserializeAsync nullable return type? #60195
-
The The documentation says
But the actual return type is I'm currently throwing a var mediaInfo = await JsonSerializer.DeserializeAsync<MediaInfo>(stream, cancellationToken);
if (mediaInfo is null)
{
throw new JsonException("Failed to deserialize the JSON stream as MediaInfo.");
} How are we supposed to handle |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I honestly think it would be way more practical if |
Beta Was this translation helpful? Give feedback.
-
If you pass a root literal null (the json text is Generally, you should always verify bad input. Sometimes it's correctly formed, but the content of fields are bad. Nulls can be handled in the verification, together with null fields. |
Beta Was this translation helpful? Give feedback.
If you pass a root literal null (the json text is
null
without quotation), it should be deserialized as null.Generally, you should always verify bad input. Sometimes it's correctly formed, but the content of fields are bad. Nulls can be handled in the verification, together with null fields.