@@ -139,18 +139,7 @@ public class BSONDecoder {
139
139
if let doc = document as? T {
140
140
return doc
141
141
}
142
- let _decoder = _BSONDecoder ( referencing: . document( document) , options: self . options)
143
- do {
144
- return try type. init ( from: _decoder)
145
- } catch let error as BSONErrorProtocol {
146
- let unknownErrorMessage = " Unknown Error occurred while decoding BSON "
147
- throw DecodingError . dataCorrupted (
148
- DecodingError . Context (
149
- codingPath: [ ] ,
150
- debugDescription: " Unable to decode BSON: \( error. errorDescription ?? unknownErrorMessage) "
151
- )
152
- )
153
- }
142
+ return try self . decode ( type, fromBSON: BSON . document ( document) )
154
143
}
155
144
156
145
/**
@@ -165,6 +154,29 @@ public class BSONDecoder {
165
154
try self . decode ( type, from: BSONDocument ( fromBSON: data) )
166
155
}
167
156
157
+ /**
158
+ * Decodes a top-level value of the given type from the given BSON.
159
+ *
160
+ * - Parameter type: The type of the value to decode.
161
+ * - Parameter document: The BSON to decode from.
162
+ * - Returns: A value of the requested type.
163
+ * - Throws: `DecodingError` if any value throws an error during decoding.
164
+ */
165
+ internal func decode< T: Decodable > ( _ type: T . Type , fromBSON bson: BSON ) throws -> T {
166
+ let _decoder = _BSONDecoder ( referencing: bson, options: self . options)
167
+ do {
168
+ return try _decoder. unbox ( bson, as: type)
169
+ } catch let error as BSONErrorProtocol {
170
+ let unknownErrorMessage = " Unknown Error occurred while decoding BSON "
171
+ throw DecodingError . dataCorrupted (
172
+ DecodingError . Context (
173
+ codingPath: _decoder. codingPath,
174
+ debugDescription: " Unable to decode BSON: \( error. errorDescription ?? unknownErrorMessage) "
175
+ )
176
+ )
177
+ }
178
+ }
179
+
168
180
// TODO: SWIFT-930 Implement this
169
181
// /**
170
182
// * Decodes a top-level value of the given type from the given JSON/extended JSON string.
0 commit comments