使用 JSONDecoder 解码 PascalCase JSON

Axe*_*min 1 json uppercase swift codable decodable

我需要用大写首字母(又名 PascalCase 或 UppperCamelCase)解码 JSON,如下所示:

{
    "Title": "example",
    "Items": [
      "hello",
      "world"
    ]
}
Run Code Online (Sandbox Code Playgroud)

所以我创建了一个符合以下条件的模型Codable

struct Model: Codable {
    let title: String
    let items: [String]
}
Run Code Online (Sandbox Code Playgroud)

JSONDecoder会引发错误,因为情况不同。

Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "title", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"title\", intValue: nil) (\"title\").", underlyingError: nil))
Run Code Online (Sandbox Code Playgroud)

我想将模型的属性保留在驼峰命名法中,但我无法更改 JSON 格式。

Axe*_*min 7

我发现的一个很好的解决方案是创建一个KeyDecodingStrategy类似于.convertFromSnakeCaseFoundation 中可用的解决方案。

extension JSONDecoder.KeyDecodingStrategy {
    static var convertFromPascalCase: JSONDecoder.KeyDecodingStrategy {
        return .custom { keys -> CodingKey in
            // keys array is never empty
            let key = keys.last!
            // Do not change the key for an array
            guard key.intValue == nil else {
                return key
            }

            let codingKeyType = type(of: key)
            let newStringValue = key.stringValue.firstCharLowercased()

            return codingKeyType.init(stringValue: newStringValue)!
        }
    }
}

private extension String {
    func firstCharLowercased() -> String {
        prefix(1).lowercased() + dropFirst()
    }
}
Run Code Online (Sandbox Code Playgroud)

它可以像这样轻松使用:

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromPascalCase
let model = try! decoder.decode(Model.self, from: json)
Run Code Online (Sandbox Code Playgroud)

Gist上的完整示例