-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathJSONArrayWrapper.swift
More file actions
40 lines (31 loc) · 1000 Bytes
/
JSONArrayWrapper.swift
File metadata and controls
40 lines (31 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import Foundation
public typealias JSONArray = [JSONDictionary]
public struct JSONArrayWrapper: Codable {
public let jsonArray: JSONArray
public enum CodingKeys: String, CodingKey {
case jsonArray
}
public init(jsonArray: JSONArray) {
self.jsonArray = jsonArray
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let data = try container.decode(Data.self, forKey: CodingKeys.jsonArray)
let object = try JSONSerialization.jsonObject(
with: data,
options: []
)
guard let jsonArray = object as? JSONArray else {
throw StorageError.decodingFailed
}
self.jsonArray = jsonArray
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
let data = try JSONSerialization.data(
withJSONObject: jsonArray,
options: []
)
try container.encode(data, forKey: CodingKeys.jsonArray)
}
}