CodingKeys within a Struct

Hi,

I was wondering, is there a way when defining CodingKeys to omit having to define a case for every property, ideally I would just want to override the property in question and leave the others as their default.

struct MyStruct: Decodable {
    let subjectId: String
    let name: String
    let dob: String
    
    enum CodingKeys: String, CodingKey {
        case subjectId = "SubjectIdentifier"
    }
}

The above does not compile unless I add the remaining cases for name and dob

No, synthesis for CodingKeys is all-or-nothing at the moment. The way I usually work around this is to define some private stored properties with the 'wire' names for the properties in question and then declare the internal/public interface with computed properties as necessary:

e.g.,

struct MyStruct: Decodable {
    let subjectId: String { SubjectIdentifier }
    let name: String
    let dob: String

    private let SubjectIdentifier: String
}
2 Likes