Using Non-Nullable Reference Types is great, but there seems to be no built-in support for enforcing Non-Nullability when compiling a class with nullable=enable and using something like XML or JSON deserialization. I know it's just compiler attributes that get set, but isn't there any easy way to make sure a deserialized object didn't have some (maybe nested!) properties that shouldn't be null
, but are?
If you explicitly set a non-nullable reference property to null
in your JSON, Newtonsoft.JSON and System.Text.Json will happily use that as a property value. I'm using Records everywhere to get around annoying initalization procedure and it works for serializing/deserializing, but it never actually enforces that deserialized properties are not null
. :(
I don't want to check for null
everywhere I use the properties - that's what I'm using Non-Nullable Reference Types for in the first place!
I'm thinking about writing a recursive reflection function to validate objects retrieved this way, but surely somebody must have already though of that problem?