4

The problem: decode an array of objects belonging to Parent and Child classes.

I read a lot of stuff on this subject but I have not been able to find a simple solution.

I encoded a type property which provide the information of the original class, but I haven't found a way to use it in decoding the object.

class Parent: Codable, CustomDebugStringConvertible {
    var debugDescription: String {
        return "[\(name)]"
    }
    
    var name: String
    init(name: String) {
        self.name = name
    }
    
    enum CodingKeys: CodingKey {
        case name
        case type
        case age
    }
    
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try! container.decode(String.self, forKey: .name)
        let type = try! container.decode(String.self, forKey: .type)
        print("Reading \(type)")
        
        if type == "Child" {
            try Child.init(from: decoder)
            return
        }
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("Parent", forKey: .type)
        try container.encode(name, forKey: .name)
    }
}


class Child: Parent {
    
    override var debugDescription: String {
        return "[\(name) - \(age)]"
    }
    var age: Int

    init(name: String, age: Int) {
        self.age = age
        super.init(name: name)
    }
    
    enum CodingKeys: CodingKey {
        case name
        case age
        case type
    }
    
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        age = try! container.decode(Int.self, forKey: .age)
        let name = try! container.decode(String.self, forKey: .name)
        super.init(name: name) // I think the problem is here!
    }
    
    override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("Child", forKey: .type)
        try container.encode(age, forKey: .age)
    }
}

This is the test code.


let array = [Parent(name: "p"), Child(name: "c",age: 2)]
print(array)
        
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let decoder = JSONDecoder()
do {
   let jsonData = try encoder.encode(array)
   let s = String(data: jsonData, encoding: .ascii)
   print("Json Data \(s!)")
            
    let decodedArray = try decoder.decode([Parent].self, from: jsonData)
            
    print(decodedArray)
 }
 catch {
    print(error.localizedDescription)
 }

The output of the original array is:

[[p], [c - 2]]

The output of the decode array is:

[[p], [c]]

How do I change the Parent and/or the Child init function in order to correctly decode the object?

Clearly, my actual scenario is much more complex of this. I have to encode / decode a class which contains an array of classes with inheritance. I have tried to use this:

https://github.com/IgorMuzyka/Type-Preserving-Coding-Adapter

Apparently, it works fine on an array of Parent, Child but it doesn't if the array is inside another class.

Moreover, I would like to learn a solution to reuse in other cases and avoid including external library is not strictly needed.

Fab
  • 1,468
  • 1
  • 16
  • 37
  • I don't see anywhere in this code that you set the `age` variable of the Child. After calling `super.init(name: name)`, I think you should call self.init(name: name, age: age)`. You should also drop the `name` argument in `self.init` which is redundant because it's already set in the superclass initialiser. – andrija Oct 03 '20 at 09:52
  • Adding self.init(name: name, age: age) after super.init(name: name) I get this error Initializer cannot both delegate ('self.init') and chain to a superclass initializer ('super.init'). 2) – Fab Oct 03 '20 at 12:28
  • Right, then call just `self.init(name: name, age: age)` , since you have `super` call inside that convenience initialiser. – andrija Oct 03 '20 at 13:14

1 Answers1

7

I think a major part of the problem is that you are using an array of mixed types, [Any], and then you are decoding it as one type Parent because it is quite possible to get the child objects to be properly encoded as Child.

One solution is to create a new Codable struct that holds the array and that with the use of a type property keeps track on how to decode the objects in the array

enum ObjectType: String, Codable {
    case parent
    case child
}

struct ParentAndChild: Codable {
    let objects: [Parent]

    enum CodingKeys: CodingKey {
        case objects
    }

    enum ObjectTypeKey: CodingKey {
        case type
    }

    init(with objects: [Parent]) {
        self.objects = objects
    }

    init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: CodingKeys.self)
            var objectsArray = try container.nestedUnkeyedContainer(forKey: CodingKeys.objects)
            var items = [Parent]()

        var array = objectsArray
        while !objectsArray.isAtEnd {
            let object = try objectsArray.nestedContainer(keyedBy: ObjectTypeKey.self)
            let type = try object.decode(ObjectType.self, forKey: ObjectTypeKey.type)
            switch type {
            case .parent:
                items.append(try array.decode(Parent.self))
            case .child:
                items.append(try array.decode(Child.self))
            }
        }
        self.objects = items
    }
}

I have also made some changes to the classes as well, the Parent class is hugely simplified and the Child class has modified functionality for encoding/decoding where the main change is that init(from:) calls supers init(from:)

class Parent: Codable, CustomDebugStringConvertible {
    var debugDescription: String {
        return "[\(name)]"
    }

    var name: String
    init(name: String) {
        self.name = name
    }
}

class Child: Parent {

    override var debugDescription: String {
        return "[\(name) - \(age)]"
    }
    var age: Int

    init(name: String, age: Int) {
        self.age = age
        super.init(name: name)
    }

    enum CodingKeys: CodingKey {
        case age
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        age = try container.decode(Int.self, forKey: .age)
        try super.init(from: decoder)
    }

    override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(age, forKey: .age)
    }
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Thank for your answer. Unfortunately, in my real code I have about 30 different different Child classes, so splitting the array by class type is not an option. – Fab Oct 03 '20 at 11:51
  • @Fab I have posted a new solution – Joakim Danielson Oct 03 '20 at 12:45
  • @Fab Why don't you make your parent class into a protocol, and make the child classes implement it, that would be a proper way to do this, and you would circumvent the problem with decoding. – andrija Oct 03 '20 at 13:16
  • I was also working on a similar solution, I don't like it much but I think that it's the only option available. To make it working, though, I had to add *** try container.encode(ObjectType.parent, forKey: .type) *** and *** try container.encode(ObjectType.child, forKey: .type) *** in the encode function of respective classes. Answer accepted, thank you. – Fab Oct 03 '20 at 13:16