-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathJSValue.swift
More file actions
197 lines (187 loc) · 5.95 KB
/
JSValue.swift
File metadata and controls
197 lines (187 loc) · 5.95 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
public import JSON
@frozen public struct JSValue {
@usableFromInline let storage: Storage
@inlinable init(storage: Storage) {
self.storage = storage
}
}
extension JSValue {
@inlinable public static func boolean(_ value: Bool) -> Self {
.init(storage: .boolean(value))
}
@inlinable public static func string(_ value: JSString) -> Self {
.init(storage: .string(value))
}
@inlinable public static func number(_ value: Double) -> Self {
.init(storage: .number(value))
}
@inlinable public static func object(_ value: JSObject) -> Self {
.init(storage: .object(value))
}
@inlinable public static var null: Self {
.init(storage: .null)
}
@inlinable public static var undefined: Self {
.init(storage: .undefined)
}
@inlinable public static func symbol(_ value: JSSymbol) -> Self {
.init(storage: .symbol(value))
}
@inlinable public static func bigInt(_ value: JSBigInt) -> Self {
.init(storage: .bigInt(value))
}
}
extension JSValue: JSONEncodable {
public func encode(to json: inout JSON) {
switch self.storage {
case .null:
(nil as Never?).encode(to: &json)
case .boolean(let js):
js.encode(to: &json)
case .string(let js):
js.string.encode(to: &json)
case .number(let double):
// ``Double`` can only represent integers up to 2^53, so any integer larger than
// that would have been represented as ``JSBigInt`` instead
if let integer: Int64 = .init(exactly: double) {
integer.encode(to: &json)
} else {
double.encode(to: &json)
}
case .object(let js):
js.encode(to: &json)
case .bigInt(let js):
js.int128.encode(to: &json)
case .undefined:
// we wouldn’t want to encode null here, that’s different, and we can’t throw an
// error either. doing nothing would still produce invalid JSON though. so trapping
// is the least bad behavior.
fatalError("undefined is not a valid JSON value")
case .symbol:
// the initializer for ``JSSymbol`` is internal, so it should be unreachable
fatalError("symbols are not a valid JSON value")
}
}
}
extension JSValue: JSONDecodable {
public init(json: borrowing JSON.Node) throws {
switch json {
case .null:
self = .null
case .bool(let json):
self = .boolean(json)
case .string(let json):
self = .string(JSString.init(json.value))
case .number(let json):
switch json {
case .fallback(let string):
if let int128: Int128 = .init(string) {
self = .number(int128)
} else {
self = .number(parsing: string)
}
case .infinity(.plus):
self = .number(.infinity)
case .infinity(.minus):
self = .number(-.infinity)
case .inline(let json):
if let int128: Int128 = json.as(Int128.self) {
self = .number(int128)
} else {
self = .number(parsing: "\(json)")
}
case .nan:
self = .number(.nan)
case .snan:
self = .number(.signalingNaN)
}
case .object(let json):
self = .object(try JSObject.json(json))
case .array(let json):
self = .object(try JSObject.json(json))
}
}
}
extension JSValue {
private static func number(_ int128: Int128) -> JSValue {
if let double: Double = .init(exactly: int128) {
return .number(double)
} else {
return .bigInt(JSBigInt.init(int128: int128))
}
}
private static func number(parsing string: String) -> JSValue {
if let double: Double = .init(string) {
return .number(double)
} else {
// this should have never passed parser validation in the first place
fatalError("Unable to reparse JSON number to Double?!")
}
}
}
extension JSValue: ConstructibleFromJSValue {
@inlinable public static func construct(from value: JSValue) -> Self? { value }
}
extension JSValue: ConvertibleToJSValue {
@inlinable public var jsValue: JSValue { self }
}
extension JSValue {
@inlinable public var number: Double? {
guard case .number(let value) = self.storage else {
return nil
}
return value
}
@inlinable public var bigInt: JSBigInt? {
guard case .bigInt(let value) = self.storage else {
return nil
}
return value
}
}
extension JSValue {
@inlinable public var jsString: JSString? {
guard case .string(let value) = self.storage else {
return nil
}
return value
}
}
extension JSValue {
@inlinable public var boolean: Bool? {
guard case .boolean(let value) = self.storage else {
return nil
}
return value
}
@inlinable public var string: String? {
guard case .string(let value) = self.storage else {
return nil
}
return value.string
}
@inlinable public var object: JSObject? {
guard case .object(let value) = self.storage else {
return nil
}
return value
}
@inlinable public var symbol: JSSymbol? {
guard case .symbol(let value) = self.storage else {
return nil
}
return value
}
@inlinable public var isNull: Bool {
guard case .null = self.storage else {
return false
}
return true
}
@inlinable public var isUndefined: Bool {
guard case .undefined = self.storage else {
return false
}
return true
}
}