0

I have a JSON result from the server that looks like the following:

let json = """
    {

        "type": "rating",
        "data": {
            "maxRating": 5,
            "isDarkMode": true
    }
}
"""

The value for data can be any key-values. I want to map this JSON to my Swift model. So, I implemented the following:

struct Model: Decodable {
    let type: String
    let data: [String: Any]
    
    private enum CodingKeys: String, CodingKey {
        case type
        case data
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.type = try container.decode(String.self, forKey: .type)
        self.data = try container.decode([String: Any].self, forKey: .data) // THIS 
    }
}

But on self.data = try container.decode ... it gives me the following error:

 error: no exact matches in call to instance method 'decode'
        self.data = try container.decode([String: Any].self, forKey: .data)

How can I fix it?

Mary Doe
  • 1,073
  • 1
  • 6
  • 22
  • have a look at this SO post: https://stackoverflow.com/questions/44603248/how-to-decode-a-property-with-type-of-json-dictionary-in-swift-45-decodable-pr – workingdog support Ukraine Jan 06 '22 at 05:53
  • What do you mean that they can be any key-values? Isn’t it depending on what request you make, because if it is we can probably find an easier solution than trying to work around Any? – Joakim Danielson Jan 06 '22 at 08:11

3 Answers3

0

[String : Any] is not decodable. You can not decode with Any. But there have other solutions. Here is one https://dev.to/absoftware/how-to-make-swift-s-string-any-decodable-5c6n

Make change in the Metadata as -

enum DataType: String, Codable {
    case payload
    case metadata
}

struct Payload: Codable {
    let id: String
    let eventName: String
    let metadata: [Metadata]
}

struct Metadata: Codable {
    let maxRating: Int?
    let isDarkMode: Bool?
    // Add other variables that may appear from your JSON
}

enum MyValue: Decodable {
    case payload(_ payload: Payload)
    case metadata(_ metadata: Metadata)

    private enum CodingKeys: String, CodingKey {
        case `type`
        case `data`
    }

    init(from decoder: Decoder) throws {
        let map = try decoder.container(keyedBy: CodingKeys.self)
        let dataType = try map.decode(DataType.self, forKey: .type)
        switch dataType {
        case .payload:
            self = .payload(try map.decode(Payload.self, forKey: .data))
        case .metadata:
            self = .metadata(try map.decode(Metadata.self, forKey: .data))
        }
    }
}
Imran0001
  • 580
  • 4
  • 11
0

Use the following Extension

JSONCodingKeys.swift

import Foundation

// Inspired by https://gist.github.com/mbuchetics/c9bc6c22033014aa0c550d3b4324411a

struct JSONCodingKeys: CodingKey {
    var stringValue: String

    init?(stringValue: String) {
        self.stringValue = stringValue
    }

    var intValue: Int?

    init?(intValue: Int) {
        self.init(stringValue: "\(intValue)")
        self.intValue = intValue
    }
}


extension KeyedDecodingContainer {

    func decode(_ type: Dictionary<String, Any>.Type, forKey key: K) throws -> Dictionary<String, Any> {
        let container = try self.nestedContainer(keyedBy: JSONCodingKeys.self, forKey: key)
        return try container.decode(type)
    }

    func decodeIfPresent(_ type: Dictionary<String, Any>.Type, forKey key: K) throws -> Dictionary<String, Any>? {
        guard contains(key) else {
            return nil
        }
        return try decode(type, forKey: key)
    }

    func decode(_ type: Array<Any>.Type, forKey key: K) throws -> Array<Any> {
        var container = try self.nestedUnkeyedContainer(forKey: key)
        return try container.decode(type)
    }

    func decodeIfPresent(_ type: Array<Any>.Type, forKey key: K) throws -> Array<Any>? {
        guard contains(key) else {
            return nil
        }
        return try decode(type, forKey: key)
    }

    func decode(_ type: Dictionary<String, Any>.Type) throws -> Dictionary<String, Any> {
        var dictionary = Dictionary<String, Any>()

        for key in allKeys {
            if let boolValue = try? decode(Bool.self, forKey: key) {
                dictionary[key.stringValue] = boolValue
            } else if let stringValue = try? decode(String.self, forKey: key) {
                dictionary[key.stringValue] = stringValue
            } else if let intValue = try? decode(Int.self, forKey: key) {
                dictionary[key.stringValue] = intValue
            } else if let doubleValue = try? decode(Double.self, forKey: key) {
                dictionary[key.stringValue] = doubleValue
            } else if let nestedDictionary = try? decode(Dictionary<String, Any>.self, forKey: key) {
                dictionary[key.stringValue] = nestedDictionary
            } else if let nestedArray = try? decode(Array<Any>.self, forKey: key) {
                dictionary[key.stringValue] = nestedArray
            }
        }
        return dictionary
    }
}

extension UnkeyedDecodingContainer {

    mutating func decode(_ type: Array<Any>.Type) throws -> Array<Any> {
        var array: [Any] = []
        while isAtEnd == false {
            if let value = try? decode(Bool.self) {
                array.append(value)
            } else if let value = try? decode(Double.self) {
                array.append(value)
            } else if let value = try? decode(String.self) {
                array.append(value)
            } else if let nestedDictionary = try? decode(Dictionary<String, Any>.self) {
                array.append(nestedDictionary)
            } else if let nestedArray = try? decode(Array<Any>.self) {
                array.append(nestedArray)
            }
        }
        return array
    }

    mutating func decode(_ type: Dictionary<String, Any>.Type) throws -> Dictionary<String, Any> {

        let nestedContainer = try self.nestedContainer(keyedBy: JSONCodingKeys.self)
        return try nestedContainer.decode(type)
    }
}

How to use.

struct Model: Decodable {
    let type: String
    let data: [String: Any]
    
    private enum CodingKeys: String, CodingKey {
        case type
        case data
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.type = try container.decode(String.self, forKey: .type)
        self.data = try container.decode([String: Any].self, forKey: .data)
    }
}

let decoder = JSONDecoder()

let userJson = """
    {
        "type": "rating",
        "data": {
            "maxRating": 5,
            "isDarkMode": true
        }
    }
""".data(using: .utf8)!

let user = try! decoder.decode(Model.self, from: userJson)
print(user)

Original answer from

Sreekuttan
  • 1,579
  • 13
  • 19
0

You have to create different struct for data parameter. You can refer below struct for reference

import Foundation
struct JsonResponse : Codable {
    let type : String?
    let data : Data?

    enum CodingKeys: String, CodingKey {

        case type = "type"
        case data = "data"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        type = try values.decodeIfPresent(String.self, forKey: .type)
        data = try values.decodeIfPresent(Data.self, forKey: .data)
    }

}

struct Data : Codable {
    let maxRating : Int?
    let isDarkMode : Bool?

    enum CodingKeys: String, CodingKey {

        case maxRating = "maxRating"
        case isDarkMode = "isDarkMode"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        maxRating = try values.decodeIfPresent(Int.self, forKey: .maxRating)
        isDarkMode = try values.decodeIfPresent(Bool.self, forKey: .isDarkMode)
    }

}
Harshal Pathak
  • 757
  • 1
  • 6
  • 18