从 Swift 中的多个服务解码 JSON 的更简单方法
Simpler method for decoding JSON from multiple services in Swift
前提
我有一个符合 Decodable
的 struct
,因此它可以通过 init(from:)
从各种响应中解码 JSON。对于我希望解码的每种类型的 JSON 响应,我都有一个符合 CodingKey
.
的 enum
例子
这是一个简化的例子,可以放到 Swift 游乐场中:
import Foundation
// MARK: - Services -
struct Service1 {}
struct Service2 {}
// MARK: - Person Model -
struct Person {
let name: String
}
extension Person: Decodable {
enum CodingKeys: String, CodingKey {
case name = "name"
}
enum Service2CodingKeys: String, CodingKey {
case name = "person_name"
}
// And so on through service n...
init(from decoder: Decoder) throws {
switch decoder.userInfo[.service] {
case is Service1.Type:
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
case is Service2.Type:
let container = try decoder.container(keyedBy: Service2CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
// And so on through service n...
default:
fatalError("Missing implementation for service.")
}
}
}
// MARK: - CodingUserInfoKey -
extension CodingUserInfoKey {
static let service = CodingUserInfoKey(rawValue: "service")!
}
// MARK: - Responses -
// The JSON response from service 1.
let service1JSONResponse = """
[
{
"name": "Peter",
}
]
""".data(using: .utf8)!
// The JSON response from service 2.
let service2JSONResponse = """
[
{
"person_name": "Paul",
}
]
""".data(using: .utf8)!
// And so on through service n... where other services have JSON responses with keys of varied names ("full_name", "personName").
// MARK: - Decoding -
let decoder = JSONDecoder()
decoder.userInfo[.service] = Service1.self
let service1Persons = try decoder.decode([Person].self, from: service1JSONResponse)
decoder.userInfo[.service] = Service2.self
let service2Persons = try decoder.decode([Person].self, from: service2JSONResponse)
问题
我 运行 遇到的问题是,我有许多不同的服务需要解码来自的响应,并且模型的属性比这个简化的示例多得多。随着服务数量的增加,解码这些响应所需的案例数量也会增加。
问题
如何简化我的 init(from:)
实现以减少所有这些代码重复?
尝试次数
我尝试为每个服务存储正确的 CodingKey.Type
并将其传递到 container(keyedBy:)
,但我收到此错误:
Cannot invoke 'container' with an argument list of type '(keyedBy: CodingKey.Type)'.
init(from decoder: Decoder) throws {
let codingKeyType: CodingKey.Type
switch decoder.userInfo[.service] {
case is Service1.Type: codingKeyType = CodingKeys.self
case is Service2.Type: codingKeyType = Service2CodingKeys.self
default: fatalError("Missing implementation for service.")
}
let container = try decoder.container(keyedBy: codingKeyType) // ← Error
name = try container.decode(String.self, forKey: .name)
}
如果 Person
的 init(from:) 中没有一堆自定义的每服务(或每服务类型)功能,我认为这很难做到。您不能将符合 CodingKey
的自定义枚举传递给 decoder.container(keyedBy:)
,因为它在该枚举的类型上是通用的。
您可以做到这一点的一种方法是使用自定义密钥解码策略并从字典或通过自定义密钥解码中的函数执行映射 method/closure。
在下面的示例中,我使用枚举来表示服务。映射字典以枚举大小写为键,因此反映了 service/service 类型的键映射。希望这可以作为您更复杂的实际用例的有用路线图。
import Foundation
// MARK: - Custom Key Decoding -
struct MyCodingKey: CodingKey {
var stringValue: String
var intValue: Int?
init?(stringValue: String) {
self.stringValue = stringValue
self.intValue = nil
}
init?(intValue: Int) {
self.stringValue = String(intValue)
self.intValue = intValue
}
}
// MARK: - Services -
enum Services: String {
case service1
case service2
}
extension Services {
var mapping: [String:String] {
switch self {
case .service1: return [:]
case .service2: return ["person_name": "name"]
}
}
func getPersons(jsonData: Data) throws -> [Person] {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .custom { (keys: [CodingKey]) -> CodingKey in
let lastKey = keys.last!
guard lastKey.intValue == nil else {
return MyCodingKey(intValue: lastKey.intValue!)!
}
guard let stringValue = self.mapping[lastKey.stringValue] else {
return lastKey
}
return MyCodingKey(stringValue: stringValue)!
}
let persons = try decoder.decode([Person].self, from: jsonData)
return persons
}
}
// MARK: - Person Model -
struct Person: Decodable {
let name: String
}
// MARK: - Responses -
// The JSON response from service 1.
let service1JSONResponse = """
[ { "name": "Peter", } ]
""".data(using: .utf8)!
// The JSON response from service 2.
let service2JSONResponse = """
[ { "person_name": "Paul", } ]
""".data(using: .utf8)!
// MARK: - Sample Calls -
print((try? Services.service1.getPersons(jsonData: service1JSONResponse))!)
print((try? Services.service2.getPersons(jsonData: service2JSONResponse))!)
与其尝试使用 CodingKeys 和越来越复杂的 init
来解决这个问题,我建议通过协议来组合它:
protocol PersonLoader: Decodable {
var name: String { get }
// additional properties
}
extension Person {
init(loader: PersonLoader) {
self.name = loader.name
// additional properties, but this is one-time
}
}
或者,特别是当 Person 是一个只读的简单数据对象时,您可以将 Person 设为一个协议,然后就可以避免这个额外的复制步骤。
然后您可以为每个服务单独定义接口:
struct Service1Person: PersonLoader {
let name: String
}
struct Service2Person: PersonLoader {
let person_name: String
var name: String { person_name }
}
完成后映射到 Persons:
let service2Persons = try decoder.decode([Service2Person].self,
from: service2JSONResponse)
.map(Person.init)
如果您采用仅协议方法,它会看起来像这样:
protocol Person: Decodable {
var name: String { get }
// additional properties
}
struct Service1Person: Person {
let name: String
}
struct Service2Person: Person {
var name: String { person_name }
let person_name: String
}
let service2Personsx = try decoder.decode([Service2Person].self,
from: service2JSONResponse) as [Person]
前提
我有一个符合 Decodable
的 struct
,因此它可以通过 init(from:)
从各种响应中解码 JSON。对于我希望解码的每种类型的 JSON 响应,我都有一个符合 CodingKey
.
enum
例子
这是一个简化的例子,可以放到 Swift 游乐场中:
import Foundation
// MARK: - Services -
struct Service1 {}
struct Service2 {}
// MARK: - Person Model -
struct Person {
let name: String
}
extension Person: Decodable {
enum CodingKeys: String, CodingKey {
case name = "name"
}
enum Service2CodingKeys: String, CodingKey {
case name = "person_name"
}
// And so on through service n...
init(from decoder: Decoder) throws {
switch decoder.userInfo[.service] {
case is Service1.Type:
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
case is Service2.Type:
let container = try decoder.container(keyedBy: Service2CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
// And so on through service n...
default:
fatalError("Missing implementation for service.")
}
}
}
// MARK: - CodingUserInfoKey -
extension CodingUserInfoKey {
static let service = CodingUserInfoKey(rawValue: "service")!
}
// MARK: - Responses -
// The JSON response from service 1.
let service1JSONResponse = """
[
{
"name": "Peter",
}
]
""".data(using: .utf8)!
// The JSON response from service 2.
let service2JSONResponse = """
[
{
"person_name": "Paul",
}
]
""".data(using: .utf8)!
// And so on through service n... where other services have JSON responses with keys of varied names ("full_name", "personName").
// MARK: - Decoding -
let decoder = JSONDecoder()
decoder.userInfo[.service] = Service1.self
let service1Persons = try decoder.decode([Person].self, from: service1JSONResponse)
decoder.userInfo[.service] = Service2.self
let service2Persons = try decoder.decode([Person].self, from: service2JSONResponse)
问题
我 运行 遇到的问题是,我有许多不同的服务需要解码来自的响应,并且模型的属性比这个简化的示例多得多。随着服务数量的增加,解码这些响应所需的案例数量也会增加。
问题
如何简化我的 init(from:)
实现以减少所有这些代码重复?
尝试次数
我尝试为每个服务存储正确的 CodingKey.Type
并将其传递到 container(keyedBy:)
,但我收到此错误:
Cannot invoke 'container' with an argument list of type '(keyedBy: CodingKey.Type)'.
init(from decoder: Decoder) throws {
let codingKeyType: CodingKey.Type
switch decoder.userInfo[.service] {
case is Service1.Type: codingKeyType = CodingKeys.self
case is Service2.Type: codingKeyType = Service2CodingKeys.self
default: fatalError("Missing implementation for service.")
}
let container = try decoder.container(keyedBy: codingKeyType) // ← Error
name = try container.decode(String.self, forKey: .name)
}
如果 Person
的 init(from:) 中没有一堆自定义的每服务(或每服务类型)功能,我认为这很难做到。您不能将符合 CodingKey
的自定义枚举传递给 decoder.container(keyedBy:)
,因为它在该枚举的类型上是通用的。
您可以做到这一点的一种方法是使用自定义密钥解码策略并从字典或通过自定义密钥解码中的函数执行映射 method/closure。
在下面的示例中,我使用枚举来表示服务。映射字典以枚举大小写为键,因此反映了 service/service 类型的键映射。希望这可以作为您更复杂的实际用例的有用路线图。
import Foundation
// MARK: - Custom Key Decoding -
struct MyCodingKey: CodingKey {
var stringValue: String
var intValue: Int?
init?(stringValue: String) {
self.stringValue = stringValue
self.intValue = nil
}
init?(intValue: Int) {
self.stringValue = String(intValue)
self.intValue = intValue
}
}
// MARK: - Services -
enum Services: String {
case service1
case service2
}
extension Services {
var mapping: [String:String] {
switch self {
case .service1: return [:]
case .service2: return ["person_name": "name"]
}
}
func getPersons(jsonData: Data) throws -> [Person] {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .custom { (keys: [CodingKey]) -> CodingKey in
let lastKey = keys.last!
guard lastKey.intValue == nil else {
return MyCodingKey(intValue: lastKey.intValue!)!
}
guard let stringValue = self.mapping[lastKey.stringValue] else {
return lastKey
}
return MyCodingKey(stringValue: stringValue)!
}
let persons = try decoder.decode([Person].self, from: jsonData)
return persons
}
}
// MARK: - Person Model -
struct Person: Decodable {
let name: String
}
// MARK: - Responses -
// The JSON response from service 1.
let service1JSONResponse = """
[ { "name": "Peter", } ]
""".data(using: .utf8)!
// The JSON response from service 2.
let service2JSONResponse = """
[ { "person_name": "Paul", } ]
""".data(using: .utf8)!
// MARK: - Sample Calls -
print((try? Services.service1.getPersons(jsonData: service1JSONResponse))!)
print((try? Services.service2.getPersons(jsonData: service2JSONResponse))!)
与其尝试使用 CodingKeys 和越来越复杂的 init
来解决这个问题,我建议通过协议来组合它:
protocol PersonLoader: Decodable {
var name: String { get }
// additional properties
}
extension Person {
init(loader: PersonLoader) {
self.name = loader.name
// additional properties, but this is one-time
}
}
或者,特别是当 Person 是一个只读的简单数据对象时,您可以将 Person 设为一个协议,然后就可以避免这个额外的复制步骤。
然后您可以为每个服务单独定义接口:
struct Service1Person: PersonLoader {
let name: String
}
struct Service2Person: PersonLoader {
let person_name: String
var name: String { person_name }
}
完成后映射到 Persons:
let service2Persons = try decoder.decode([Service2Person].self,
from: service2JSONResponse)
.map(Person.init)
如果您采用仅协议方法,它会看起来像这样:
protocol Person: Decodable {
var name: String { get }
// additional properties
}
struct Service1Person: Person {
let name: String
}
struct Service2Person: Person {
var name: String { person_name }
let person_name: String
}
let service2Personsx = try decoder.decode([Service2Person].self,
from: service2JSONResponse) as [Person]