-
Notifications
You must be signed in to change notification settings - Fork 2
/
BoxedJSON.swift
51 lines (45 loc) · 1.3 KB
/
BoxedJSON.swift
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
41
42
43
44
45
46
47
48
49
50
51
import Foundation
import RichJSONParser
internal class BoxedJSON {
enum Value {
case null
case boolean(Bool)
case number(String)
case string(String)
case array([BoxedJSON])
case object(JSONDictionary<BoxedJSON>)
init(_ json: JSON) {
switch json {
case .null: self = .null
case .boolean(let b): self = .boolean(b)
case .number(let n): self = .number(n)
case .string(let s): self = .string(s)
case .array(let a):
self = .array(a.map { $0.box() })
case .object(let o):
self = .object(o.mapValues { $0.box() })
}
}
}
var value: Value
init(_ value: Value) {
self.value = value
}
func unbox() -> JSON {
switch value {
case .null: return .null
case .boolean(let b): return .boolean(b)
case .number(let n): return .number(n)
case .string(let s): return .string(s)
case .array(let a):
return .array(a.map { $0.unbox() })
case .object(let o):
return .object(o.mapValues { $0.unbox() })
}
}
}
extension JSON {
func box() -> BoxedJSON {
return BoxedJSON(BoxedJSON.Value(self))
}
}