1

I have the following structure nested in a structure that is returned from an service call but I can't manage to encode/decode this part. The problem I am having is that the customKey and customValue are both dynamic. customValue may be array or dictionary or array of dictionary For Example

{
    "status": "a string value",
    "id": "a string value",
    "property": {
        "customkey1": "customValue1",
        "customKey2": [{
            "InnerCustomKey": "InnerCustomValue"
        }, {
            "InnerCustomKey": "InnerCustomValue"
        }, {
            "InnerCustomKey": "InnerCustomValue"
        }],
        "customkey3": {
            "InnerCustomKey": "InnerCustomValue"
        }
    }
}

I tried something like var values: [String:String] But it failed in another object. I follow the answer Using Codable on a dynamic type/object but not getting success

3
  • Are options for customValue [reasonable] finite? Commented Nov 15, 2019 at 13:43
  • Are those all variants that are possible (String, Array, Dictionary, Array of Dictionary) and are all innermost values of type String? Commented Nov 15, 2019 at 15:21
  • @user28434 customValue have any value (String, Array, Dictionary, Array of Dictionary etc.) Commented Nov 16, 2019 at 5:46

1 Answer 1

2
extension KeyedDecodingContainerProtocol{

    func getValueFromAvailableKey(codingKeys:[CodingKey])-> Any?{
        for key in codingKeys{
            for keyPath in self.allKeys{
                if key.stringValue == keyPath.stringValue{
                    do{
                        if let value = try? self.decodeIfPresent([String].self, forKey:keyPath){
                            return value
                        }

                        if let value = try? self.decodeIfPresent([String:String].self, forKey:keyPath){
                            return value
                        }

                        if let value = try? self.decodeIfPresent([[String:String]].self, forKey:keyPath){
                            return value
                        }

                        if let value = try? self.decodeIfPresent(String.self, forKey:keyPath){
                            return value
                        }

                        return nil
                    }
                }
            }
        }
        return nil
    }
}

private struct CustomCodingKeys: CodingKey {
    var stringValue: String
    init?(stringValue: String) {
        self.stringValue = stringValue
    }
    var intValue: Int?
    init?(intValue: Int) {
        return nil
    }
}

struct CustomModel: Codable {
    var status:String?
    var id: String?
    var property: Property?


    init(from decoder: Decoder)  {
        do{
            let container = try decoder.container(keyedBy: CustomCodingKeys.self)
            status = try container.decodeIfPresent(String.self, forKey: CustomCodingKeys.init(stringValue: "status")!)
            id = try container.decodeIfPresent(String.self, forKey: CustomCodingKeys.init(stringValue: "id")!)
            property = try container.decodeIfPresent(Property.self, forKey: CustomCodingKeys.init(stringValue: "property")!)


        }catch{
            print(error.localizedDescription)
        }
    }

    func encode(to encoder: Encoder) throws {
             var container = encoder.container(keyedBy: CustomCodingKeys.self)
             try? container.encodeIfPresent(self.status, forKey: CustomCodingKeys.init(stringValue: "status")!)
             try? container.encodeIfPresent(self.id, forKey: CustomCodingKeys.init(stringValue: "id")!)
             try? container.encodeIfPresent(self.property, forKey: CustomCodingKeys.init(stringValue: "property")!)
    }
}

// MARK: - Property
struct Property: Codable {
    var customkey:[String:Any?] = [:]

    init(from decoder: Decoder)  {
        do{
            let container = try decoder.container(keyedBy: CustomCodingKeys.self)
            for key in container.allKeys{
                customkey[key.stringValue] = container.getValueFromAvailableKey(codingKeys: [CustomCodingKeys.init(stringValue: key.stringValue)!])
            }

        }catch{
            print(error.localizedDescription)
        }
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CustomCodingKeys.self)
        for key in customkey{
                 try? container.encodeIfPresent(customkey[key.key] as? String, forKey: CustomCodingKeys.init(stringValue: key.key)!)
                 try? container.encodeIfPresent(customkey[key.key] as? [[String:String]], forKey: CustomCodingKeys.init(stringValue: key.key)!)
                 try? container.encodeIfPresent(customkey[key.key] as? [String:String], forKey: CustomCodingKeys.init(stringValue: key.key)!)
            }

    }
}

Hope this will help you !

Sign up to request clarification or add additional context in comments.

4 Comments

your sample code was good for decode but there are no encode function Do you please add encode code also?
How do you want to encode ??? do u want to encode same json ??
yes, I first decode the json string which was done by your sample then I added some data and then encode that data to json. for that I have to extension of Property as encode
@Mahesh i have reedited the answer for encoding as well Regards,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.