forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinJSON.swift
More file actions
29 lines (22 loc) · 834 Bytes
/
JoinJSON.swift
File metadata and controls
29 lines (22 loc) · 834 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
import Foundation
public struct JoinJSON {
public init() {}
public func join(_ a: String, with b: String) -> Data {
return join(a.data(using: .utf8) ?? Data(), with: b.data(using: .utf8) ?? Data())
}
public func join(_ a: Data, with b: String) -> Data {
return join(a, with: b.data(using: .utf8) ?? Data())
}
public func join(_ a: Data, with b: Data) -> Data {
guard let firstDict = try? JSONSerialization.jsonObject(with: a) as? [String: Any],
let secondDict = try? JSONSerialization.jsonObject(with: b) as? [String: Any]
else {
return a
}
var merged = firstDict
for (key, value) in secondDict {
merged[key] = value
}
return (try? JSONSerialization.data(withJSONObject: merged)) ?? a
}
}