Make class conforming to Codable by default in Swift

10,701

Here's a good blog post which includes an answer to your question: source

Scroll down to inheritance and you'll see the following:

Assuming we have the following classes:

class Person : Codable {
    var name: String?
}

class Employee : Person {
    var employeeID: String?
}

We get the Codable conformance by inheriting from the Person class, but what happens if we try to encode an instance of Employee?

let employee = Employee()
employee.employeeID = "emp123"
employee.name = "Joe"

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try! encoder.encode(employee)
print(String(data: data, encoding: .utf8)!)

{
  "name" : "Joe"
}

This is not the expected result, so we have to add a custom implementation like this:

class Person : Codable {
    var name: String?

    private enum CodingKeys : String, CodingKey {
        case name
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
    }
}

Same story for the subclass:

class Employee : Person {
    var employeeID: String?

    private enum CodingKeys : String, CodingKey {
        case employeeID = "emp_id"
    }

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(employeeID, forKey: .employeeID)
    }
}

The result would be:

{
  "emp_id" : "emp123"
}

Which again is not the expected result, so here we are using inheritance by calling super.

// Employee.swift
    override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(employeeID, forKey: .employeeID)
    }

Which finally gives us what we really wanted from the beginning:

{
    "name": "Joe",
    "emp_id": "emp123"
}

If you're not happy with the flattened result, there's a tip on how to avoid that too.

All the credits to the guy who wrote the blog post and my thanks. Hope it helps you as well, cheers!

Share:
10,701
zzheads
Author by

zzheads

iOS developer

Updated on June 04, 2022

Comments

  • zzheads
    zzheads almost 2 years

    Such feature of Swift as Codable (Decodable&Encodable) protocol is very useful. But I found such issue: Let we have class Parent conforming to Codable:

    class Parent: Codable {
        var name: String
        var email: String?
        var password: String?
    }
    

    Ok, that class is conforming to Codable protocol "from the box", you don't need write any initializers, it's ready to be initialized from JSON like that:

    { "name": "John", "email": "[email protected]", "password": <null>}
    

    But let's say we need other class, Child inherits from Parent and be conforming to Codable:

    class Child: Parent {
       var token: String
       var date: Date?
    }
    

    so class Child must be conforming to Codable by conformance to Parent, BUT properties of class Child won't be initialized from JSON properly. Decision I found is write all Codable stuff for class Child by myself, like:

    class Child: Parent {
        var token: String
        var date : Date?
    
        enum ChildKeys: CodingKey {
            case token, date
        }
    
        required init(from decoder: Decoder) throws {
            try super.init(from: decoder)
            let container = try decoder.container(keyedBy: ChildKeys.self)
            self.token = try container.decode(String.self, forKey: .token)
            self.date = try container.decodeIfPresent(Date.self, forKey: .date)
        }
    
        override func encode(to encoder: Encoder) throws {
            try super.encode(to: encoder)
            var container = encoder.container(keyedBy: ChildKeys.self)
            try container.encode(self.token, forKey: .token)
            try container.encodeIfPresent(self.date, forKey: .date)
        }
    }
    

    But I feel it can't be right, did I missed something? How to make class Child conforming to Codable properly without writing all that stuff?