Skip to content

Commit c5343c6

Browse files
authored
Update JSON interop guide to reflect Vapor integration (#62)
1 parent 8a9d60c commit c5343c6

File tree

1 file changed

+3
-26
lines changed

1 file changed

+3
-26
lines changed

Guides/JSON-Interop.md

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,34 +90,11 @@ ContentConfiguration.global.use(decoder: decoder, for: .json)
9090
```
9191
in your `configure.swift`.
9292

93-
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-
extension ExtendedJSONEncoder: ContentEncoder {
97-
public func encode<E>(_ encodable: E, to body: 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-
extension ExtendedJSONDecoder: ContentDecoder {
108-
public func decode<D>(_ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders) throws -> D
109-
where D: Decodable
110-
{
111-
let data = body.getData(at: body.readerIndex, length: body.readableBytes) ?? Data()
112-
return try self.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.
11694

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
119-
[Examples/ComplexVaporExample](https://github.com/mongodb/mongo-swift-driver/tree/main/Examples/ComplexVaporExample).
95+
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.
12096

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).
12198
## Using `JSONEncoder` and `JSONDecoder` with BSON Types
12299

123100
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

Comments
 (0)