JSONDecoder currently does not differentiate between a payload containing an explicit null
or the field being omitted. I would like to propose an additive change to include a decoding strategy for these situations:
class JSONDecoder {
...
enum NullDecodingStrategy {
// Default, today's behavior
case implicit
// If struct contains "var anInt: Int?", valid JSON payloads must explicitly contain either {'anInt': <integer>} or {'anInt': null}. Not containing 'anInt' results in a decoding error.
// If struct contains "var anInt: Int??", {'anInt': <integer>} decodes as .some(.some(<integer>)), {'anInt': null} decodes as .some(.none), and the field being omitted results decodes as .none.
case explicit
}
var nullDecodingStrategy: NullDecodingStrategy = . implicit
...
}
(DISCLAIMER: Might need some bikeshedding.)