@@ -8,9 +8,7 @@ public class ExtendedJSONDecoder {
8
8
} ( )
9
9
10
10
/// Initialize an `ExtendedJSONDecoder`.
11
- public init ( ) {
12
- fatalError ( " unimplemented " )
13
- }
11
+ public init ( ) { }
14
12
15
13
/// Decodes an instance of the requested type `T` from the provided extended JSON data.
16
14
/// - SeeAlso: https://docs.mongodb.com/manual/reference/mongodb-extended-json/
@@ -21,10 +19,14 @@ public class ExtendedJSONDecoder {
21
19
/// - Returns: Decoded representation of the JSON input as an instance of `T`.
22
20
/// - Throws: `DecodingError` if the JSON data is corrupt or if any value throws an error during decoding.
23
21
public func decode< T: Decodable > ( _ type: T . Type , from data: Data ) throws -> T {
22
+ // Data --> JSON --> BSON --> T
24
23
// Takes in JSON as `Data` encoded with `.utf8` and runs it through a `JSONDecoder` to get an
25
- // instance of the `JSON` enum. Then a `BSON` enum instance is created via the `JSON`.
24
+ // instance of the `JSON` enum.
25
+ let wrappedData = " [ " . utf8 + data + " ] " . utf8
26
+ let json = try JSONDecoder ( ) . decode ( [ JSON ] . self, from: wrappedData) [ 0 ]
27
+ // Then a `BSON` enum instance is created via the `JSON`.
28
+ let bson = try BSON ( fromExtJSON: json, keyPath: [ ] )
26
29
// The `BSON` is then passed through a `BSONDecoder` where it is outputted as a `T`
27
- // Data --> JSON --> BSON --> T
28
- fatalError ( " unimplemented " )
30
+ return try BSONDecoder ( ) . decode ( T . self, fromBSON: bson)
29
31
}
30
32
}
0 commit comments