简体   繁体   中英

Swift `Decodable` is not not working for nested JSON Objects unless the nested is an array

I have started converting some JSON parsing code to use the new Apple Decodable protocol and have hit a blocker that feels too fundamental to have been missed during Apple's testing so I'm wondering if I'm doing something silly. In short I am trying the parse a JSON graph such as this and since I'm only decoding I thought conforming to Decodable should be enough but it seems from the errors that I need to conform to Codable (Decodable & Encodable) to get the desired decoding effect:

{"keyString": {"nestedKey1" : "value1", "nestedKey1" : "value1" } }

It works with this case:

{"keyString": [{"nestedKey1" : "value1", "nestedKey1" : "value1" } ]}

as is an array of nested objects but not for a single object.

Is this a Swift bug or am I dong something wrong?

Here is a sample playground that can prove the problem. If the Animal class conforms to Decodable it doesn't parse for the array case but if I set Animal to conform to Codable then it does work. I would not expect this to be the case since I am only decoding JSON here.

import Foundation

//class Animal: Codable {    
class Animal: Decodable {
    var fileURLPath: String = ""
    var age: Double = 0
    var height: Double = 0
    var weight: Double = 0

    private enum CodingKeys: String, CodingKey {
        case fileURLPath = "path"
        case age
        case height
        case weight
    }

    required init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        fileURLPath = try values.decode(String.self, forKey: .fileURLPath)
        age = try values.decode(TimeInterval.self, forKey: .age)
        height = try values.decode(Double.self, forKey: .height)
        weight = try values.decode(Double.self, forKey: .weight)
    }
}

let innerObjectJSON = """
{
    "path": "tiger_pic.png",
    "age": 9,
    "height": 1.23,
    "weight": 130
}
"""

let innerObjectData = innerObjectJSON.data(using: String.Encoding.utf8)

let jsonDataNestedObject = """
    { "en" : \(innerObjectJSON)
    }
    """.data(using: String.Encoding.utf8)

let jsonDataNestedArray = """
    { "en" : [\(innerObjectJSON), \(innerObjectJSON), \(innerObjectJSON) ]
    }
    """.data(using: String.Encoding.utf8)

print("Nested Array of Objects:")

do {
    let result = try JSONDecoder().decode([String: [Animal]].self, from: jsonDataNestedArray!)
    result["en"]!.forEach ({ print($0.fileURLPath) }) // This one works
} catch { print(error) }

print("\n\n Single Object:")
do {
    let result = try JSONDecoder().decode(Animal.self, from: innerObjectData!)
        print(result.fileURLPath)
} catch { print(error) }

print("\n\nNested Object:")
do {
    let result = try JSONDecoder().decode([String: Animal].self, from:jsonDataNestedObject!)
        print(result["en"]!.fileURLPath) // I would also expect this to work but I get the error: "fatal error: Dictionary<String, Animal> does not conform to Decodable because Animal does not conform to Decodable.: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.45.6/src/swift/stdlib/public/core/Codable.swift, line 3420"
} catch { print(error) }

If you do not want to wait until the next release you can use a struct :

import Cocoa

struct Animal: Codable {
    var fileURLPath: String
    var age: Double
    var height: Double
    var weight: Double

    private enum CodingKeys: String, CodingKey {
        case fileURLPath = "path"
        case age, height, weight
    }
}

let innerObjectJSON = """
{
"path": "tiger_pic.png",
"age": 9,
"height": 1.23,
"weight": 130
}
"""

let innerObjectData = innerObjectJSON.data(using: String.Encoding.utf8)

let jsonDataNestedObject = """
    { "en" : \(innerObjectJSON)
    }
    """.data(using: String.Encoding.utf8)


print("\n\nNested Object:")
do {
    let result = try JSONDecoder().decode([String: Animal].self, from:jsonDataNestedObject!)
    print(result["en"]!.fileURLPath) 
} catch { print(error) }

This gives me

Nested Object:
tiger_pic.png
["en": __lldb_expr_190.Animal(fileURLPath: "tiger_pic.png", age: 9.0, height: 1.23, weight: 130.0)]

It decodes easily, however it does not use TimeInterval which is just an alias for Double though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM