@@ -49,6 +49,7 @@ extension JSONPathType {
49
49
extension String : JSONPathType {
50
50
51
51
/// A method used to retrieve a value from a given dictionary for a specific key.
52
+ /// - parameter dictionary: A `Dictionary` with `String` keys and `JSON` values.
52
53
/// - throws: `.KeyNotFound` with an associated value of `self`, where `self` is a `String`,
53
54
/// should the key not be present within the `JSON`.
54
55
/// - returns: The `JSON` value associated with the given key.
@@ -64,6 +65,7 @@ extension String: JSONPathType {
64
65
extension Int : JSONPathType {
65
66
66
67
/// A method used to retrieve a value from a given array for a specific index.
68
+ /// - parameter array: An `Array` of `JSON`.
67
69
/// - throws: `.IndexOutOfBounds` with an associated value of `self`, where `self` is an `Int`,
68
70
/// should the index not be within the valid range for the array of `JSON`.
69
71
/// - returns: The `JSON` value found at the given index.
@@ -189,7 +191,7 @@ extension JSON {
189
191
return try JSON . getArray ( valueAtPath ( path) )
190
192
}
191
193
192
- /// Attempts to decodes many values from a desendant JSON array at a path
194
+ /// Attempts to decode many values from a descendant JSON array at a path
193
195
/// into JSON.
194
196
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
195
197
/// - parameter type: If the context this method is called from does not
@@ -211,6 +213,20 @@ extension JSON {
211
213
public func dictionary( path: JSONPathType ... ) throws -> [ Swift . String : JSON ] {
212
214
return try JSON . getDictionary ( valueAtPath ( path) )
213
215
}
216
+
217
+ /// Attempts to decode many values from a descendant JSON object at a path
218
+ /// into JSON.
219
+ /// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
220
+ /// - parameter type: If the context this method is called from does not
221
+ /// make the return type clear, pass a type implementing `JSONDecodable`
222
+ /// to disambiguate the value type to decode with.
223
+ /// - returns: A `Dictionary` of `String` keys and decoded values.
224
+ /// - throws: One of the `JSON.Error` cases thrown by `decode(_:type:)` or
225
+ /// any error that arises from decoding the contained values.
226
+ /// - seealso: `JSON.decode(_:type:)`
227
+ public func dictionaryOf< Decoded: JSONDecodable > ( path: JSONPathType ... , type: Decoded . Type = Decoded . self) throws -> [ Swift . String : Decoded ] {
228
+ return try JSON . getDictionaryOf ( valueAtPath ( path) )
229
+ }
214
230
215
231
}
216
232
@@ -376,6 +392,9 @@ extension JSON {
376
392
/// JSON.
377
393
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
378
394
/// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
395
+ /// - parameter type: If the context this method is called from does not
396
+ /// make the return type clear, pass a type implementing `JSONDecodable`
397
+ /// to disambiguate the value type to decode with.
379
398
/// - returns: An `Array` of decoded elements if found, otherwise `nil`.
380
399
/// - throws: One of the following errors contained in `JSON.Error`:
381
400
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
@@ -409,6 +428,29 @@ extension JSON {
409
428
public func dictionary( path: JSONPathType ... , alongPath options: SubscriptingOptions ) throws -> [ Swift . String : JSON ] ? {
410
429
return try mapOptionalAtPath ( path, alongPath: options, transform: JSON . getDictionary)
411
430
}
431
+
432
+ /// Optionally attempts to decode many values from a descendant object at a path
433
+ /// into JSON.
434
+ /// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
435
+ /// - parameter alongPath: Options that control what should be done with values that are `null` or keys that are missing.
436
+ /// - parameter type: If the context this method is called from does not
437
+ /// make the return type clear, pass a type implementing `JSONDecodable`
438
+ /// to disambiguate the value type to decode with.
439
+ /// - returns: A `Dictionary` of `String` mapping to decoded elements if a
440
+ /// value could be found, otherwise `nil`.
441
+ /// - throws: One of the following errors contained in `JSON.Error`:
442
+ /// * `KeyNotFound`: A key `path` does not exist inside a descendant
443
+ /// `JSON` dictionary.
444
+ /// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
445
+ /// descendant `JSON` array.
446
+ /// * `UnexpectedSubscript`: A `path` item cannot be used with the
447
+ /// corresponding `JSON` value.
448
+ /// * `TypeNotConvertible`: The target value's type inside of the `JSON`
449
+ /// instance does not match the decoded value.
450
+ /// * Any error that arises from decoding the value.
451
+ public func dictionaryOf< Decoded: JSONDecodable > ( path: JSONPathType ... , alongPath options: SubscriptingOptions , type: Decoded . Type = Decoded . self) throws -> [ Swift . String : Decoded ] ? {
452
+ return try mapOptionalAtPath ( path, alongPath: options, transform: JSON . getDictionaryOf)
453
+ }
412
454
413
455
}
414
456
@@ -436,7 +478,7 @@ extension JSON {
436
478
437
479
/// Retrieves a `Double` from a path into JSON or a fallback if not found.
438
480
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
439
- /// - parameter fallback: Array to use when one is missing at the subscript.
481
+ /// - parameter fallback: `Double` to use when one is missing at the subscript.
440
482
/// - returns: A floating-point `Double`
441
483
/// - throws: One of the `JSON.Error` cases thrown by calling `mapOptionalAtPath(_:fallback:transform:)`.
442
484
/// - seealso: `optionalAtPath(_:ifNotFound)`.
@@ -446,7 +488,7 @@ extension JSON {
446
488
447
489
/// Retrieves an `Int` from a path into JSON or a fallback if not found.
448
490
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
449
- /// - parameter fallback: Array to use when one is missing at the subscript.
491
+ /// - parameter fallback: `Int` to use when one is missing at the subscript.
450
492
/// - returns: A numeric `Int`
451
493
/// - throws: One of the following errors contained in `JSON.Error`:
452
494
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
@@ -463,7 +505,7 @@ extension JSON {
463
505
464
506
/// Retrieves a `String` from a path into JSON or a fallback if not found.
465
507
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
466
- /// - parameter fallback: Array to use when one is missing at the subscript.
508
+ /// - parameter fallback: `String` to use when one is missing at the subscript.
467
509
/// - returns: A textual `String`
468
510
/// - throws: One of the following errors contained in `JSON.Error`:
469
511
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
@@ -480,7 +522,7 @@ extension JSON {
480
522
481
523
/// Retrieves a `Bool` from a path into JSON or a fallback if not found.
482
524
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
483
- /// - parameter fallback: Array to use when one is missing at the subscript.
525
+ /// - parameter fallback: `Bool` to use when one is missing at the subscript.
484
526
/// - returns: A truthy `Bool`
485
527
/// - throws: One of the following errors contained in `JSON.Error`:
486
528
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
@@ -497,7 +539,7 @@ extension JSON {
497
539
498
540
/// Retrieves a `[JSON]` from a path into JSON or a fallback if not found.
499
541
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
500
- /// - parameter fallback: Array to use when one is missing at the subscript.
542
+ /// - parameter fallback: ` Array` to use when one is missing at the subscript.
501
543
/// - returns: An `Array` of `JSON` elements
502
544
/// - throws: One of the following errors contained in `JSON.Error`:
503
545
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
@@ -515,7 +557,7 @@ extension JSON {
515
557
/// Attempts to decodes many values from a desendant JSON array at a path
516
558
/// into the recieving structure, returning a fallback if not found.
517
559
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
518
- /// - parameter fallback: Array to use when one is missing at the subscript.
560
+ /// - parameter fallback: ` Array` to use when one is missing at the subscript.
519
561
/// - returns: An `Array` of decoded elements
520
562
/// - throws: One of the following errors contained in `JSON.Error`:
521
563
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
@@ -534,7 +576,7 @@ extension JSON {
534
576
/// Retrieves a `[String: JSON]` from a path into JSON or a fallback if not
535
577
/// found.
536
578
/// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
537
- /// - parameter fallback: Value to use when one is missing at the subscript
579
+ /// - parameter fallback: `Dictionary` to use when one is missing at the subscript.
538
580
/// - returns: An `Dictionary` of `String` mapping to `JSON` elements
539
581
/// - throws: One of the following errors contained in `JSON.Error`:
540
582
/// * `KeyNotFound`: A key `path` does not exist inside a descendant
@@ -549,6 +591,26 @@ extension JSON {
549
591
return try mapOptionalAtPath ( path, fallback: fallback, transform: JSON . getDictionary)
550
592
}
551
593
594
+ /// Attempts to decode many values from a descendant JSON object at a path
595
+ /// into the receiving structure, returning a fallback if not found.
596
+ /// - parameter path: 0 or more `String` or `Int` that subscript the `JSON`
597
+ /// - parameter fallback: Value to use when one is missing at the subscript
598
+ /// - returns: A `Dictionary` of `String` mapping to decoded elements.
599
+ /// - throws: One of the following errors contained in `JSON.Error`:
600
+ /// * `KeyNotFound`: A key `path` does not exist inside a descendant
601
+ /// `JSON` dictionary.
602
+ /// * `IndexOutOfBounds`: An index `path` is outside the bounds of a
603
+ /// descendant `JSON` array.
604
+ /// * `UnexpectedSubscript`: A `path` item cannot be used with the
605
+ /// corresponding `JSON` value.
606
+ /// * `TypeNotConvertible`: The target value's type inside of the `JSON`
607
+ /// instance does not match the decoded value.
608
+ /// * Any error that arises from decoding the value.
609
+ public func dictionaryOf< Decoded: JSONDecodable > ( path: JSONPathType ... , @autoclosure or fallback: ( ) -> [ Swift . String : Decoded ] ) throws -> [ Swift . String : Decoded ] {
610
+ return try mapOptionalAtPath ( path, fallback: fallback, transform: JSON . getDictionaryOf)
611
+ }
612
+
613
+
552
614
}
553
615
554
616
// MARK: - Deprecated methods
0 commit comments