Consider .NET 6's System.Text.Json.JsonSerializer.Deserialize method and its many overloads.
Each one returns either object?
or TValue?
which indicates that null
could be returned. However, the documentation states that in the event of any error, an exception is thrown. Moreover, it also states that a valid value is returned (if an exception isn't thrown). Nowhere does it state that null
could be returned. And in my experience of using it, null
is never returned.
(The nullable return type is a problem if you turn on "nullability" in your project, because the compiler then starts warning you that the returned value could be null - which in my experience (and according to the documentation) just isn't true. It's this sort of thing that has me scurrying to switch nullability off again. I know I can suppress the warning, but I don't think that's the point.)
Is the documentation lacking? Can the methods really return null? If they can't, then why do we have nullable return types?
null
is valid JSON. – Fairfax