-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathJSONSubscripting.swift
More file actions
613 lines (546 loc) · 31.8 KB
/
JSONSubscripting.swift
File metadata and controls
613 lines (546 loc) · 31.8 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//
// JSONSubscripting.swift
// Freddy
//
// Created by Zachary Waldowski on 8/15/15.
// Copyright © 2015 Big Nerd Ranch. All rights reserved.
//
// MARK: JSONPathType
/// A protocol used to define a path within an instance of `JSON` that leads to some desired value.
///
/// A custom type, such as a `RawRepresentable` enum, may be made to conform to `JSONPathType`
/// and used with the subscript APIs.
public protocol JSONPathType {
/// Use `self` to key into a `dictionary`.
///
/// Unlike Swift dictionaries, failing to find a value for a key should throw
/// an error rather than convert to `nil`.
///
/// Upon failure, implementers should throw an error from `JSON.Error`.
func value(in dictionary: [String : JSON]) throws -> JSON
/// Use `self` to index into an `array`.
///
/// Unlike Swift arrays, attempting to index outside the collection's bounds
/// should throw an error rather than crash.
///
/// Upon failure, implementers should throw an error from `JSON.Error`.
func value(in array: [JSON]) throws -> JSON
}
extension JSONPathType {
/// The default behavior for keying into a dictionary is to throw
/// `JSON.Error.UnexpectedSubscript`.
public func value(in dictionary: [String : JSON]) throws -> JSON {
throw JSON.Error.unexpectedSubscript(type: Self.self)
}
/// The default behavior for indexing into an array is to throw
/// `JSON.Error.UnexpectedSubscript`.
public func value(in array: [JSON]) throws -> JSON {
throw JSON.Error.unexpectedSubscript(type: Self.self)
}
}
extension String: JSONPathType {
/// A method used to retrieve a value from a given dictionary for a specific key.
/// - parameter dictionary: A `Dictionary` with `String` keys and `JSON` values.
/// - throws: `.KeyNotFound` with an associated value of `self`, where `self` is a `String`,
/// should the key not be present within the `JSON`.
/// - returns: The `JSON` value associated with the given key.
public func value(in dictionary: [String : JSON]) throws -> JSON {
guard let next = dictionary[self] else {
throw JSON.Error.keyNotFound(key: self)
}
return next
}
}
extension Int: JSONPathType {
/// A method used to retrieve a value from a given array for a specific index.
/// - parameter array: An `Array` of `JSON`.
/// - throws: `.IndexOutOfBounds` with an associated value of `self`, where `self` is an `Int`,
/// should the index not be within the valid range for the array of `JSON`.
/// - returns: The `JSON` value found at the given index.
public func value(in array: [JSON]) throws -> JSON {
guard case array.indices = self else {
throw JSON.Error.indexOutOfBounds(index: self)
}
return array[self]
}
}
// MARK: - Subscripting core
private extension JSON {
enum SubscriptError: Swift.Error {
case subscriptIntoNull(JSONPathType)
}
func value(for pathFragment: JSONPathType, detectingNull: Bool) throws -> JSON {
switch self {
case .null where detectingNull:
throw SubscriptError.subscriptIntoNull(pathFragment)
case let .dictionary(dict):
return try pathFragment.value(in: dict)
case let .array(array):
return try pathFragment.value(in: array)
default:
throw Error.unexpectedSubscript(type: type(of: pathFragment))
}
}
func value(at path: [JSONPathType], detectingNull: Bool = false) throws -> JSON {
var result = self
for pathFragment in path {
result = try result.value(for: pathFragment, detectingNull: detectingNull)
}
return result
}
}
// MARK: - Subscripting operator
extension JSON {
public subscript(key: String) -> JSON? {
return try? value(for: key, detectingNull: false)
}
public subscript(index: Int) -> JSON? {
return try? value(for: index, detectingNull: false)
}
}
// MARK: - Simple member unpacking
extension JSON {
/// Attempts to decode into the returning type from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`.
/// - parameter type: If the context this method is called from does not
/// make the return type clear, pass a type implementing `JSONDecodable`
/// to disambiguate the type to decode with.
/// - returns: An initialized member from the inner JSON.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A given `String` key does not exist inside a
/// descendant `JSON` dictionary.
/// * `IndexOutOfBounds`: A given `Int` index is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A given subscript cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of
/// the `JSON` instance does not match `Decoded`.
public func decode<Decoded: JSONDecodable>(at path: JSONPathType..., type: Decoded.Type = Decoded.self) throws -> Decoded {
return try Decoded(json: value(at: path))
}
/// Retrieves a `Double` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - returns: A floating-point `Double`
/// - throws: One of the `JSON.Error` cases thrown by `decode(at:type:)`.
/// - seealso: `JSON.decode(at:type:)`
public func getDouble(at path: JSONPathType...) throws -> Double {
return try Double(json: value(at: path))
}
/// Retrieves an `Int` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - returns: A numeric `Int`
/// - throws: One of the `JSON.Error` cases thrown by `decode(at:type:)`.
/// - seealso: `JSON.decode(at:type:)`
public func getInt(at path: JSONPathType...) throws -> Int {
return try Int(json: value(at: path))
}
/// Retrieves a `String` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - returns: A textual `String`
/// - throws: One of the `JSON.Error` cases thrown by `decode(at:type:)`.
/// - seealso: `JSON.decode(at:type:)`
public func getString(at path: JSONPathType...) throws -> String {
return try String(json: value(at: path))
}
/// Retrieves a `Bool` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - returns: A truthy `Bool`
/// - throws: One of the `JSON.Error` cases thrown by `decode(at:type:)`.
/// - seealso: `JSON.decode(at:type:)`
public func getBool(at path: JSONPathType...) throws -> Bool {
return try Bool(json: value(at: path))
}
/// Retrieves a `[JSON]` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - returns: An `Array` of `JSON` elements
/// - throws: One of the `JSON.Error` cases thrown by `decode(at:type:)`.
/// - seealso: `JSON.decode(at:type:)`
public func getArray(at path: JSONPathType...) throws -> [JSON] {
return try JSON.getArray(from: value(at: path))
}
/// Attempts to decode many values from a descendant JSON array at a path
/// into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter type: If the context this method is called from does not
/// make the return type clear, pass a type implementing `JSONDecodable`
/// to disambiguate the type to decode with.
/// - returns: An `Array` of decoded elements
/// - throws: One of the `JSON.Error` cases thrown by `decode(at:type:)`, or
/// any error that arises from decoding the contained values.
/// - seealso: `JSON.decode(at:type:)`
public func decodedArray<Decoded: JSONDecodable>(at path: JSONPathType..., type: Decoded.Type = Decoded.self) throws -> [Decoded] {
return try JSON.decodedArray(from: value(at: path))
}
/// Retrieves a `[String: JSON]` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - returns: An `Dictionary` of `String` mapping to `JSON` elements
/// - throws: One of the `JSON.Error` cases thrown by `decode(at:type:)`.
/// - seealso: `JSON.decode(at:type:)`
public func getDictionary(at path: JSONPathType...) throws -> [String: JSON] {
return try JSON.getDictionary(from: value(at: path))
}
/// Attempts to decode many values from a descendant JSON object at a path
/// into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter type: If the context this method is called from does not
/// make the return type clear, pass a type implementing `JSONDecodable`
/// to disambiguate the value type to decode with.
/// - returns: A `Dictionary` of `String` keys and decoded values.
/// - throws: One of the `JSON.Error` cases thrown by `decode(at:type:)` or
/// any error that arises from decoding the contained values.
/// - seealso: `JSON.decode(at:type:)`
public func decodedDictionary<Decoded: JSONDecodable>(at path: JSONPathType..., type: Decoded.Type = Decoded.self) throws -> [String: Decoded] {
return try JSON.decodedDictionary(from: value(at: path))
}
}
// MARK: - NotFound-Or-Null-to-Optional unpacking
extension JSON {
/// An `OptionSetType` used to represent the different options available for subscripting `JSON` with `null` values or missing keys.
/// * `.NullBecomesNil` - Treat `null` values as `nil`.
/// * `.MissingKeyBecomesNil` - Treat missing keys as `nil`.
public struct SubscriptingOptions: OptionSet {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}
/// Treat `null` values as `nil`.
public static let NullBecomesNil = SubscriptingOptions(rawValue: 1 << 0)
/// Treat missing keys as `nil`.
public static let MissingKeyBecomesNil = SubscriptingOptions(rawValue: 1 << 1)
}
fileprivate func mapOptional<Value>(at path: [JSONPathType], alongPath options: SubscriptingOptions, transform: (JSON) throws -> Value) throws -> Value? {
let detectNull = options.contains(.NullBecomesNil)
let detectNotFound = options.contains(.MissingKeyBecomesNil)
var json: JSON?
do {
json = try value(at: path, detectingNull: detectNull)
return try json.map(transform)
} catch Error.indexOutOfBounds where detectNotFound {
return nil
} catch Error.keyNotFound where detectNotFound {
return nil
} catch Error.valueNotConvertible where detectNull && json == .null {
return nil
} catch SubscriptError.subscriptIntoNull where detectNull {
return nil
}
}
}
extension JSON {
/// Decodes a `JSON` instance if it is not `.Null`, throws otherwise.
/// - parameter json: An instance of `JSON`.
/// - returns: An instance of some type that conforms to `JSONDecodable`.
/// - throws: `Error.ValueNotConvertible` if the `JSON` instance is `.Null`.
private static func getDecoded<Decoded: JSONDecodable>(json: JSON) throws -> Decoded {
guard json != .null else {
throw Error.valueNotConvertible(value: json, to: Decoded.self)
}
return try Decoded.init(json: json)
}
/// Optionally decodes into the returning type from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
/// - parameter type: If the context this method is called from does not
/// make the return type clear, pass a type implementing `JSONDecodable`
/// to disambiguate the type to decode with.
/// - returns: A decoded value from the inner JSON if found, or `nil`.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
/// * Any error that arises from decoding the value.
public func decode<Decoded: JSONDecodable>(at path: JSONPathType..., alongPath options: SubscriptingOptions, type: Decoded.Type = Decoded.self) throws -> Decoded? {
return try mapOptional(at: path, alongPath: options, transform: JSON.getDecoded)
}
/// Optionally retrieves a `Double` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`.
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
/// - returns: A `Double` if a value could be found, otherwise `nil`.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getDouble(at path: JSONPathType..., alongPath options: SubscriptingOptions) throws -> Double? {
return try mapOptional(at: path, alongPath: options, transform: Double.init)
}
/// Optionally retrieves a `Int` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
/// - returns: A numeric `Int` if a value could be found, otherwise `nil`.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getInt(at path: JSONPathType..., alongPath options: SubscriptingOptions) throws -> Int? {
return try mapOptional(at: path, alongPath: options, transform: Int.init)
}
/// Optionally retrieves a `String` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
/// - returns: A text `String` if a value could be found, otherwise `nil`.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getString(at path: JSONPathType..., alongPath options: SubscriptingOptions) throws -> String? {
return try mapOptional(at: path, alongPath: options, transform: String.init)
}
/// Optionally retrieves a `Bool` from a path into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
/// - returns: A truthy `Bool` if a value could be found, otherwise `nil`.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getBool(at path: JSONPathType..., alongPath options: SubscriptingOptions) throws -> Bool? {
return try mapOptional(at: path, alongPath: options, transform: Bool.init)
}
/// Optionally retrieves a `[JSON]` from a path into the recieving structure.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
/// - returns: An `Array` of `JSON` elements if a value could be found,
/// otherwise `nil`.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getArray(at path: JSONPathType..., alongPath options: SubscriptingOptions) throws -> [JSON]? {
return try mapOptional(at: path, alongPath: options, transform: JSON.getArray)
}
/// Optionally decodes many values from a descendant array at a path into
/// JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
/// - parameter type: If the context this method is called from does not
/// make the return type clear, pass a type implementing `JSONDecodable`
/// to disambiguate the value type to decode with.
/// - returns: An `Array` of decoded elements if found, otherwise `nil`.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
/// * Any error that arises from decoding the value.
public func decodedArray<Decoded: JSONDecodable>(at path: JSONPathType..., alongPath options: SubscriptingOptions, type: Decoded.Type = Decoded.self) throws -> [Decoded]? {
return try mapOptional(at: path, alongPath: options, transform: JSON.decodedArray)
}
/// Optionally retrieves a `[String: JSON]` from a path into the recieving
/// structure.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
/// - returns: A `Dictionary` of `String` mapping to `JSON` elements if a
/// value could be found, otherwise `nil`.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getDictionary(at path: JSONPathType..., alongPath options: SubscriptingOptions) throws -> [String: JSON]? {
return try mapOptional(at: path, alongPath: options, transform: JSON.getDictionary)
}
/// Optionally attempts to decode many values from a descendant object at a path
/// into JSON.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
/// - parameter type: If the context this method is called from does not
/// make the return type clear, pass a type implementing `JSONDecodable`
/// to disambiguate the value type to decode with.
/// - returns: A `Dictionary` of `String` mapping to decoded elements if a
/// value could be found, otherwise `nil`.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
/// * Any error that arises from decoding the value.
public func decodedDictionary<Decoded: JSONDecodable>(at path: JSONPathType..., alongPath options: SubscriptingOptions, type: Decoded.Type = Decoded.self) throws -> [String: Decoded]? {
return try mapOptional(at: path, alongPath: options, transform: JSON.decodedDictionary)
}
}
// MARK: - Missing-with-fallback unpacking
extension JSON {
fileprivate func mapOptional<Value>(at path: [JSONPathType], fallback: () -> Value, transform: (JSON) throws -> Value) throws -> Value {
return try mapOptional(at: path, alongPath: .MissingKeyBecomesNil, transform: transform) ?? fallback()
}
/// Attempts to decode into the returning type from a path into
/// JSON, or returns a fallback if not found.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter fallback: Value to use when one is missing at the subscript.
/// - returns: An initialized member from the inner JSON.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `UnexpectedSubscript`: A given subscript cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of
/// the `JSON` instance does not match `Decoded`.
public func decode<Decoded: JSONDecodable>(at path: JSONPathType..., or fallback: @autoclosure() -> Decoded) throws -> Decoded {
return try mapOptional(at: path, fallback: fallback, transform: Decoded.init)
}
/// Retrieves a `Double` from a path into JSON or a fallback if not found.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter fallback: `Double` to use when one is missing at the subscript.
/// - returns: A floating-point `Double`
/// - throws: One of the `JSON.Error` cases thrown by calling `mapOptional(at:fallback:transform:)`.
public func getDouble(at path: JSONPathType..., or fallback: @autoclosure() -> Double) throws -> Double {
return try mapOptional(at: path, fallback: fallback, transform: Double.init)
}
/// Retrieves an `Int` from a path into JSON or a fallback if not found.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter fallback: `Int` to use when one is missing at the subscript.
/// - returns: A numeric `Int`
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getInt(at path: JSONPathType..., or fallback: @autoclosure() -> Int) throws -> Int {
return try mapOptional(at: path, fallback: fallback, transform: Int.init)
}
/// Retrieves a `String` from a path into JSON or a fallback if not found.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter fallback: `String` to use when one is missing at the subscript.
/// - returns: A textual `String`
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getString(at path: JSONPathType..., or fallback: @autoclosure() -> String) throws -> String {
return try mapOptional(at: path, fallback: fallback, transform: String.init)
}
/// Retrieves a `Bool` from a path into JSON or a fallback if not found.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter fallback: `Bool` to use when one is missing at the subscript.
/// - returns: A truthy `Bool`
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getBool(at path: JSONPathType..., or fallback: @autoclosure() -> Bool) throws -> Bool {
return try mapOptional(at: path, fallback: fallback, transform: Bool.init)
}
/// Retrieves a `[JSON]` from a path into JSON or a fallback if not found.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter fallback: `Array` to use when one is missing at the subscript.
/// - returns: An `Array` of `JSON` elements
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getArray(at path: JSONPathType..., or fallback: @autoclosure() -> [JSON]) throws -> [JSON] {
return try mapOptional(at: path, fallback: fallback, transform: JSON.getArray)
}
/// Attempts to decodes many values from a desendant JSON array at a path
/// into the recieving structure, returning a fallback if not found.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter fallback: `Array` to use when one is missing at the subscript.
/// - returns: An `Array` of decoded elements
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
/// * Any error that arises from decoding the value.
public func decodedArray<Decoded: JSONDecodable>(at path: JSONPathType..., or fallback: @autoclosure() -> [Decoded]) throws -> [Decoded] {
return try mapOptional(at: path, fallback: fallback, transform: JSON.decodedArray)
}
/// Retrieves a `[String: JSON]` from a path into JSON or a fallback if not
/// found.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter fallback: `Dictionary` to use when one is missing at the subscript.
/// - returns: An `Dictionary` of `String` mapping to `JSON` elements
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
public func getDictionary(at path: JSONPathType..., or fallback: @autoclosure() -> [String: JSON]) throws -> [String: JSON] {
return try mapOptional(at: path, fallback: fallback, transform: JSON.getDictionary)
}
/// Attempts to decode many values from a descendant JSON object at a path
/// into the receiving structure, returning a fallback if not found.
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
/// - parameter fallback: Value to use when one is missing at the subscript
/// - returns: A `Dictionary` of `String` mapping to decoded elements.
/// - throws: One of the following errors contained in `JSON.Error`:
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
/// `JSON` dictionary.
/// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
/// descendant `JSON` array.
/// * `UnexpectedSubscript`: A `path` item cannot be used with the
/// corresponding `JSON` value.
/// * `TypeNotConvertible`: The target value's type inside of the `JSON`
/// instance does not match the decoded value.
/// * Any error that arises from decoding the value.
public func decodedDictionary<Decoded: JSONDecodable>(at path: JSONPathType..., or fallback: @autoclosure() -> [String: Decoded]) throws -> [String: Decoded] {
return try mapOptional(at: path, fallback: fallback, transform: JSON.decodedDictionary)
}
}