Swift 4 JSON解码器的任何方式都不会抛出包含无法识别的枚举值的数组?

时间:2017-08-17 07:38:37

标签: json decoder swift4

我正在尝试使用Swift 4的新JSON解码来解析远程服务器上的JSON。 JSON模式包含枚举值,其中一些我实际上并不需要我的目的,我想忽略。另外,我还希望足够强大,以便在JSON架构发生变化时,我仍然能够尽可能多地读取数据。

问题在于,当我尝试解析包含枚举的任何数组时,除非每个枚举值与我的枚举文字完全匹配,否则解码器会抛出异常而不是跳过它无法解析的数据。

这是一个简单的例子:

enum Size: String, Codable {
    case large = "large"
    case small = "small"
}

enum Label: String, Codable {
    case kitchen = "kitchen"
    case bedroom = "bedroom"
    case livingRoom = "living room"
}

struct Box: Codable {
    var contents: String
    var size: Size
    var labels: [Label]
}

当我解析完全符合我的Size枚举的数据时,我得到了预期的结果:

let goodJson = """
[
  {
    "contents": "pillows",
    "size": "large",
    "labels": [
        "bedroom"
    ]
  },
  {
    "contents": "books",
    "size": "small",
    "labels": [
        "bedroom",
        "living room"
    ]
  }
]
""".data(using: .utf8)!

let goodBoxes = try? JSONDecoder().decode([Box?].self, from: goodJson)
// returns [{{contents "pillows", large, [bedroom]}},{{contents "books", small, [bedroom, livingRoom]}}]

但是,如果有不符合枚举的内容,解码器会抛出异常,我什么也得不回。

let badJson = """
[
  {
    "contents": "pillows",
    "size": "large",
    "labels": [
        "bedroom"
    ]
  },
  {
    "contents": "books",
    "size": "small",
    "labels": [
        "bedroom",
        "living room",
        "office"
    ]
  },
  {
    "contents": "toys",
    "size": "medium",
    "labels": [
        "bedroom"
    ]
  }
]
""".data(using: .utf8)!

let badBoxes = try? JSONDecoder().decode([Box?].self, from: badJson)    // returns nil

理想情况下,在这种情况下,我想取回尺寸符合“小”或“大”的2个物品,第二个物品伤口有2个有效标签,“卧室”和“客厅”。 / p>

如果我为Box实现自己的init(来自:解码器),我可以自己解码标签并丢弃任何不符合我的枚举的标签。但是,我无法弄清楚如何解码[Box]类型以忽略无效的框而不实现我自己的解码器并自己解析JSON,这违背了使用Codable的目的。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我承认这不是最漂亮的解决方案,但它是我想出来的,我想我会分享。我在Array上创建了一个扩展,允许这样做。最大的问题是你必须再次解码然后编码JSON数据。

extension Array where Element: Codable {
    public static func decode(_ json: Data) throws -> [Element] {
        let jsonDecoder = JSONDecoder()
        var decodedElements: [Element] = []
        if let jsonObject = (try? JSONSerialization.jsonObject(with: json, options: [])) as? Array<Any> {
            for json in jsonObject {
               if let data = try? JSONSerialization.data(withJSONObject: json, options: []), let element = (try? jsonDecoder.decode(Element.self, from: data)) {
                    decodedElements.append(element)
                }
            }
        }
        return decodedElements
    }
}

您可以将此扩展程序与符合Codable的任何内容一起使用,并解决您的问题。

[Box].decode(json)

很遗憾,我不知道如何解决标签不正确的问题,您必须按照说法进行操作并覆盖init(from: Decoder)以确保标签有效。

答案 1 :(得分:0)

有点痛苦,但是您可以自己编写解码。

import Foundation

enum Size: String, Codable {
    case large = "large"
    case small = "small"
}

enum Label: String, Codable {
    case kitchen = "kitchen"
    case bedroom = "bedroom"
    case livingRoom = "living room"
}

struct Box: Codable {
    var contents: String = ""
    var size: Size = .small
    var labels: [Label] = []

    init(from decoder: Decoder) throws {
        guard let container = try? decoder.container(keyedBy: CodingKeys.self) else {
            return
        }

        contents = try container.decode(String.self, forKey: .contents)
        let rawSize = try container.decode(Size.RawValue.self, forKey: .size)
        size = Size(rawValue: rawSize) ?? .small

        var labelsContainer = try container.nestedUnkeyedContainer(forKey: .labels)
        while !labelsContainer.isAtEnd {
            let rawLabel = try labelsContainer.decode(Label.RawValue.self)
            if let label = Label(rawValue: rawLabel) {
                labels.append(label)
            }
        }
    }
}

extension Box: CustomStringConvertible {
    var description: String {
        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted
        do {
            let data = try encoder.encode(self)
            if let jsonString = String(data: data, encoding: .utf8) {
                return jsonString
            }
        } catch {
            return ""
        }
        return ""
    }
}

let badJson = """
[
  {
    "contents": "pillows",
    "size": "large",
    "labels": [
        "bedroom"
    ]
  },
  {
    "contents": "books",
    "size": "small",
    "labels": [
        "bedroom",
        "living room",
        "office"
    ]
  },
  {
    "contents": "toys",
    "size": "medium",
    "labels": [
        "bedroom"
    ]
  }
]
""".data(using: .utf8)!

do {
    let badBoxes = try JSONDecoder().decode([Box].self, from: badJson)
    print(badBoxes)
} catch {
    print(error)
}

输出:

[{
  "labels" : [
    "bedroom"
  ],
  "size" : "large",
  "contents" : "pillows"
}, {
  "labels" : [
    "bedroom",
    "living room"
  ],
  "size" : "small",
  "contents" : "books"
}, {
  "labels" : [
    "bedroom"
  ],
  "size" : "small",
  "contents" : "toys"
}]