You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order for this to work, you will also have to include extensions that ensure conformance to Vapor's
94
-
`ContentEncoder` and `ContentDecoder` protocols. The snippets below should be sufficient for doing that.
95
-
```swift
96
-
extensionExtendedJSONEncoder: ContentEncoder {
97
-
publicfuncencode<E>(_encodable: E, tobody: inout ByteBuffer, headers: inout HTTPHeaders) throws
98
-
where E: Encodable
99
-
{
100
-
headers.contentType= .json
101
-
try body.writeBytes(self.encode(encodable))
102
-
}
103
-
}
104
-
```
105
-
106
-
```swift
107
-
extensionExtendedJSONDecoder: ContentDecoder {
108
-
publicfuncdecode<D>(_decodable: D.Type, frombody: ByteBuffer, headers: HTTPHeaders) throws-> D
109
-
where D: Decodable
110
-
{
111
-
let data = body.getData(at: body.readerIndex, length: body.readableBytes) ??Data()
112
-
returntryself.decode(D.self, from: data)
113
-
}
114
-
}
115
-
```
93
+
Note that in order for this to work, `ExtendedJSONEncoder` must conform to Vapor's `ContentEncoder` protocol, and `ExtendedJSONDecoder` must conform to Vapor's `ContentDecoder` protocol.
116
94
117
-
To see some example Vapor apps using the driver, check out
118
-
[Examples/VaporExample](https://github.com/mongodb/mongo-swift-driver/tree/main/Examples/VaporExample) or
We've added these conformances in our Vapor integration library [MongoDBVapor](https://github.com/mongodb/mongodb-vapor), which we highly recommend using if you want to use the driver with Vapor.
120
96
97
+
To see an example Vapor app using the driver via the integration library, check out [Examples/VaporExample](https://github.com/mongodb/mongo-swift-driver/tree/main/Examples/VaporExample).
121
98
## Using `JSONEncoder` and `JSONDecoder` with BSON Types
122
99
123
100
Currently, some BSON types (e.g. `BSONBinary`) do not support working with encoders and decoders other than those introduced in `swift-bson`, meaning Foundation's `JSONEncoder` and `JSONDecoder` will throw errors when encoding or decoding such types. There are plans to add general `Codable` support for all BSON types in the future, though. For now, only `BSONObjectID` and any BSON types defined in Foundation or the standard library (e.g. `Date` or `Int32`) will work with other encoder/decoder pairs. If type information is not required in the output JSON and only types that include a general `Codable` conformance are included in your data, you can use `JSONEncoder` and `JSONDecoder` to produce and ingest JSON data.
0 commit comments