Skip to content

Commit 5ec233e

Browse files
authored
Merge pull request #32 from hartbit/json-array-dictionary
Add JSONMappable conformance to Array and Dictionary
2 parents 634c31c + 6f4a1c5 commit 5ec233e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Sources/TSCBasic/JSONMapper.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,21 @@ extension Double: JSONMappable {
152152
self = double
153153
}
154154
}
155+
156+
extension Array where Element: JSONMappable {
157+
public init(json: JSON) throws {
158+
guard case .array(let array) = json else {
159+
throw JSON.MapError.custom(key: nil, message: "expected array, got \(json)")
160+
}
161+
self = try array.map({ try Element(json: $0) })
162+
}
163+
}
164+
165+
extension Dictionary where Key == String, Value: JSONMappable {
166+
public init(json: JSON) throws {
167+
guard case .dictionary(let dictionary) = json else {
168+
throw JSON.MapError.custom(key: nil, message: "expected dictionary, got \(json)")
169+
}
170+
self = try dictionary.mapValues({ try Value(json: $0) })
171+
}
172+
}

0 commit comments

Comments
 (0)