1
1
import ExtrasJSON
2
2
import Foundation
3
+ import NIO
3
4
4
5
/// `ExtendedJSONDecoder` facilitates the decoding of ExtendedJSON into `Decodable` values.
5
6
public class ExtendedJSONDecoder {
@@ -42,19 +43,13 @@ public class ExtendedJSONDecoder {
42
43
/// Initialize an `ExtendedJSONDecoder`.
43
44
public init ( ) { }
44
45
45
- /// Decodes an instance of the requested type `T` from the provided extended JSON data.
46
- /// - SeeAlso: https://docs.mongodb.com/manual/reference/mongodb-extended-json/
47
- ///
48
- /// - Parameters:
49
- /// - type: Codable type to decode the input into.
50
- /// - data: `Data` which represents the JSON that will be decoded.
51
- /// - Returns: Decoded representation of the JSON input as an instance of `T`.
52
- /// - Throws: `DecodingError` if the JSON data is corrupt or if any value throws an error during decoding.
53
- public func decode< T: Decodable > ( _: T . Type , from data: Data ) throws -> T {
46
+ private func decodeBytes< T: Decodable , C: Collection > ( _: T . Type , from bytes: C ) throws -> T
47
+ where C. Element == UInt8
48
+ {
54
49
// Data --> JSONValue --> BSON --> T
55
50
// Takes in JSON as `Data` encoded with `.utf8` and runs it through ExtrasJSON's parser to get an
56
51
// instance of the `JSONValue` enum.
57
- let json = try JSONParser ( ) . parse ( bytes: data )
52
+ let json = try JSONParser ( ) . parse ( bytes: bytes )
58
53
59
54
// Then a `BSON` enum instance is decoded from the `JSONValue`.
60
55
let bson = try self . decodeBSONFromJSON ( json, keyPath: [ ] )
@@ -65,6 +60,38 @@ public class ExtendedJSONDecoder {
65
60
return try bsonDecoder. decode ( T . self, fromBSON: bson)
66
61
}
67
62
63
+ /// Decodes an instance of the requested type `T` from the provided extended JSON data.
64
+ /// - SeeAlso: https://docs.mongodb.com/manual/reference/mongodb-extended-json/
65
+ ///
66
+ /// - Parameters:
67
+ /// - type: Codable type to decode the input into.
68
+ /// - data: `Data` which represents the JSON that will be decoded.
69
+ /// - Returns: Decoded representation of the JSON input as an instance of `T`.
70
+ /// - Throws: `DecodingError` if the JSON data is corrupt or if any value throws an error during decoding.
71
+ public func decode< T: Decodable > ( _: T . Type , from data: Data ) throws -> T {
72
+ try self . decodeBytes ( T . self, from: data)
73
+ }
74
+
75
+ /// Decodes an instance of the requested type `T` from the provided extended JSON data.
76
+ /// - SeeAlso: https://docs.mongodb.com/manual/reference/mongodb-extended-json/
77
+ ///
78
+ /// - Parameters:
79
+ /// - type: Codable type to decode the input into.
80
+ /// - buffer: `ByteBuffer` which contains the JSON data that will be decoded.
81
+ /// - Returns: Decoded representation of the JSON input as an instance of `T`.
82
+ /// - Throws: `DecodingError` if the JSON data is corrupt or if any value throws an error during decoding.
83
+ public func decode< T: Decodable > ( _: T . Type , from buffer: ByteBuffer ) throws -> T {
84
+ guard buffer. readableBytes > 0 else {
85
+ throw DecodingError . _extendedJSONError ( keyPath: [ ] , debugDescription: " empty buffer provided to decode " )
86
+ }
87
+
88
+ var buffer = buffer
89
+ // readBytes never returns nil here because we checked that the buffer wasn't empty and only read
90
+ // readable bytes out from it.
91
+ // swiftlint:disable:next force_unwrapping
92
+ return try self . decodeBytes ( T . self, from: buffer. readBytes ( length: buffer. readableBytes) !)
93
+ }
94
+
68
95
/// Decode a `BSON` from the given extended JSON.
69
96
private func decodeBSONFromJSON( _ json: JSONValue , keyPath: [ String ] ) throws -> BSON {
70
97
switch try self . decodeScalar ( json, keyPath: keyPath) {
0 commit comments