Decoding of optionals missing in json

combining this with the idea just discussed in another thread, all in all the new opt-ins could be like so (all names are preliminary):

enum KeyDecodingStrategy {
    /// With this opt-in strategy missing in JSON optional fields with not get overridden with nil
    ///
    /// Example:
    ///  struct Foo {
    ///      var x: Int
    ///      var y: Int?
    ///      var z: Int! = 123
    ///      var w: Int? = 456
    ///  }
    ///
    ///  JSON: { "x": 1 }
    ///  Result with this strategy OFF: { x: 1, y: nil, z: nil, w: nil }
    ///  Result with this strategy ON: { x: 1, y: nil, z: Optional(123), w: Optional(456) }
    ///
    case dontOverrideFieldsThatHaveDefaultValues

    /// This strategy will require the presence of optional fields.
    ///
    /// Example:
    ///  struct Foo {
    ///      var x: Int?
    ///      var y: Int? = nil
    ///      var z: Int? = 123
    ///  }
    ///
    ///  JSON: {}
    ///  Result with this strategy OFF: { x: nil, y: nil, z: nil }   // dontOverrideFieldsThatHaveDefaultValues = off
    ///  Result with this strategy OFF: { x: nil, y: nil, z: Optional(123) }   // dontOverrideFieldsThatHaveDefaultValues = on
    ///  Result with this strategy ON: // runtime error, x & y fields are missing
    ///
    case requreFieldsThatDontHaveDefaultValues
}

enum KeyEncodingStrategy {

    /// Emit nil values
    ///
    /// Example:
    ///  struct Foo {
    ///      var x: Int
    ///      var y: Int?
    ///  }
    ///
    ///  Value { x: 1, y: nil }
    ///  Result with this strategy OFF: "{ "x": 1 }
    ///  Result with this strategy ON: "{ "x": 1, "y": nil }
    ///
    case emitExplicitNil
}
1 Like