Skip to content

Commit 5b1a553

Browse files
committed
- External JSON decoder support added per type or, you may override default
1 parent c61a5fa commit 5b1a553

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

RESTAPI/ValidResponseData/JSONCodable.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,24 @@
77
import Foundation
88
import SwiftyJSON
99

10+
public struct JSONDecoderStore {
11+
12+
private static var storage: [String: JSONDecoder] = [:]
13+
14+
public static let defaultDecoder = JSONDecoder()
15+
16+
public static subscript(_ type: JSONCodable.Type) -> JSONDecoder {
17+
get { storage[String(describing: type)] ?? defaultDecoder }
18+
set { storage[String(describing: type)] = newValue}
19+
}
20+
}
21+
1022
public protocol JSONCodable: ValidResponseData { }
1123

1224
public extension JSONCodable where Self: Decodable {
1325

1426
static func createInstance(from data: Data) throws -> Self {
15-
let decoder = JSONDecoder()
16-
return try decoder.decode(Self.self, from: data)
27+
return try JSONDecoderStore[Self].decode(Self.self, from: data)
1728
}
1829
}
1930

0 commit comments

Comments
 (0)