Swift 4 decodable with unknown dynamic keys

I have the following JSON

{"DynamicKey":6410,"Meta":{"name":"","page":""}}

DynamicKey is unknown at compile time.I'm trying to find a reference how to parse this struct using decodable.

public struct MyStruct: Decodable {
    public let unknown: Double
    public let meta: [String: String]

    private enum CodingKeys: String, CodingKey {
        case meta = "Meta"
    }
}

Any ideas?

2 answers

  • answered 2018-11-12 20:33 Rob Napier

    To decode an arbitrary string, you need a key like this:

    // Arbitrary key
    private struct Key: CodingKey, Hashable, CustomStringConvertible {
        static let meta = Key(stringValue: "Meta")!
    
        var description: String {
            return stringValue
        }
    
        var hashValue: Int { return stringValue.hash }
    
        static func ==(lhs: Key, rhs: Key) -> Bool {
            return lhs.stringValue == rhs.stringValue
        }
    
        let stringValue: String
        init(_ string: String) { self.stringValue = string }
        init?(stringValue: String) { self.init(stringValue) }
        var intValue: Int? { return nil }
        init?(intValue: Int) { return nil }
    }
    

    This is a very general-purpose tool (expect for the static let meta) that can be used for all kinds of generic-key problems.

    With that, you can find the first key that isn't .meta and use that as your dynamic key.

    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: Key.self)
    
        meta = try container.decode([String: String].self, forKey: .meta)
    
        guard let dynamicKey = container.allKeys.first(where: { $0 != .meta }) else {
            throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [],
                                                                    debugDescription: "Could not find dynamic key"))
        }
    
        unknown = try container.decode(Double.self, forKey: dynamicKey)
    }
    

    All together as a playground:

    import Foundation
    
    let json = Data("""
    {"DynamicKey":6410,"Meta":{"name":"","page":""}}
    """.utf8)
    
    public struct MyStruct: Decodable {
        public let unknown: Double
        public let meta: [String: String]
    
        // Arbitrary key
        private struct Key: CodingKey, Hashable, CustomStringConvertible {
            static let meta = Key(stringValue: "Meta")!
            var description: String {
                return stringValue
            }
    
            var hashValue: Int { return stringValue.hash }
    
            static func ==(lhs: Key, rhs: Key) -> Bool {
                return lhs.stringValue == rhs.stringValue
            }
    
            let stringValue: String
            init(_ string: String) { self.stringValue = string }
            init?(stringValue: String) { self.init(stringValue) }
            var intValue: Int? { return nil }
            init?(intValue: Int) { return nil }
        }
    
        public init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: Key.self)
    
            meta = try container.decode([String: String].self, forKey: .meta)
    
            guard let dynamicKey = container.allKeys.first(where: { $0 != .meta }) else {
                throw DecodingError.dataCorrupted(.init(codingPath: [],
                                                        debugDescription: "Could not find dynamic key"))
            }
    
            unknown = try container.decode(Double.self, forKey: dynamicKey)
        }
    }
    
    
    let myStruct = try! JSONDecoder().decode(MyStruct.self, from: json)
    myStruct.unknown
    myStruct.meta
    

    This technique can be expanded to decode arbitrary JSON. Sometimes it's easier to do that, and then pull out the pieces you want, then to decode each piece. For example, with the JSON gist above, you could implement MyStruct this way:

    public struct MyStruct: Decodable {
        public let unknown: Double
        public let meta: [String: String]
    
        public init(from decoder: Decoder) throws {
            let container = try decoder.singleValueContainer()
            let json = try container.decode(JSON.self)
    
            guard let meta = json["Meta"]?.dictionaryValue as? [String: String] else {
                throw DecodingError.dataCorrupted(.init(codingPath: [],
                                                        debugDescription: "Could not find meta key"))
            }
            self.meta = meta
    
            guard let (_, unknownJSON) = json.objectValue?.first(where: { (key, _) in key != "Meta" }),
                let unknown = unknownJSON.doubleValue
            else {
                throw DecodingError.dataCorrupted(.init(codingPath: [],
                                                        debugDescription: "Could not find dynamic key"))
            }
            self.unknown = unknown
        }
    }
    

  • answered 2018-11-12 21:03 Vyacheslav

    import UIKit
    
    var str = """
    {"DynamicKey":6410,"Meta":{"name":"","page":""}}
    """
    public struct MyStruct: Decodable {
        public var unknown: Double?
        public var meta: [String: String]?
    
        public init(from decoder: Decoder) {
    
            guard let container = try? decoder.container(keyedBy: CodingKeys.self) else {
                fatalError()
            }
                for key in container.allKeys {
                    unknown = try? container.decode(Double.self, forKey: key)//) ?? 0.0
                    if key.stringValue == "Meta" {
                        meta = try? container.decode([String: String].self, forKey: key)
                    }
    
                }
                print(container.allKeys)
        }
    
        struct CodingKeys: CodingKey {
            var stringValue: String
            init?(stringValue: String) {
                self.stringValue = stringValue
            }
            var intValue: Int?
            init?(intValue: Int) {
                return nil
            }
        }
    }
        let jsonData = str.data(using: .utf8)!
        let jsonDecoder = JSONDecoder()
        let myStruct = try! jsonDecoder.decode(MyStruct.self, from: jsonData)
        print("Meta : \(myStruct.meta)")
        print("Double : \(myStruct.unknown)")
    

    I've already answered a similar question

    https://stackoverflow.com/a/48412139/1979882