Skip to content

Commit 9911f92

Browse files
authored
Update README for SwiftNIO and Vapor 4 (#40)
1 parent 1618189 commit 9911f92

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

README.md

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ Add `pure-swift-json` as dependency to your `Package.swift`:
3535

3636
```swift
3737
dependencies: [
38-
.package(url: "https://github.com/fabianfett/pure-swift-json.git", .upToNextMajor(from: "0.1.0")),
38+
.package(url: "https://github.com/fabianfett/pure-swift-json.git", .upToNextMajor(from: "0.2.1")),
3939
],
4040
```
4141

4242
Add `PureSwiftJSONCoding` to the target you want to use it in.
4343

4444
```swift
4545
targets: [
46-
.target(
47-
name: "MyFancyTarget",
48-
dependencies: ["PureSwiftJSONCoding"]),
46+
.target(name: "MyFancyTarget", dependencies: [
47+
.product(name: "PureSwiftJSONCoding", package: "pure-swift-json"),
48+
])
4949
]
5050
```
5151

@@ -58,6 +58,70 @@ let bytesArray = try JSONEncoder().encode(myEncodable)
5858
let myDecodable = try JSONDecoder().decode(MyDecodable.self, from: bytes)
5959
```
6060

61+
### Use with SwiftNIO ByteBuffer
62+
63+
For maximal performance create an `[UInt8]` from your `ByteBuffer`, even though `buffer.readableBytesView` would technically work as well.
64+
65+
```swift
66+
let result = try pureDecoder.decode(
67+
[SampleStructure].self,
68+
from: buffer.readBytes(length: buffer.readableBytes)!)
69+
```
70+
71+
```swift
72+
let bytes = try pureEncoder.encode(encodable)
73+
var buffer = byteBufferAllocator.buffer(capacity: bytes.count)
74+
buffer.writeBytes(bytes)
75+
```
76+
77+
78+
### Use with Vapor 4
79+
80+
Increase the performance of your Vapor 4 API by using `pure-swift-json` instead of the default Foundation implementation. First you'll need to implement the conformance to Vapor's `ContentEncoder` and `ContentDecoder` as described in the [Vapor docs](https://docs.vapor.codes/4.0/content/#custom-coders).
81+
82+
```swift
83+
import Vapor
84+
import PureSwiftJSONCoding
85+
86+
extension PureSwiftJSONCoding.JSONEncoder: ContentEncoder {
87+
public func encode<E: Encodable>(
88+
_ encodable: E,
89+
to body: inout ByteBuffer,
90+
headers: inout HTTPHeaders) throws
91+
{
92+
headers.contentType = .json
93+
let bytes = try self.encode(encodable)
94+
// the buffer's storage is resized in case its capacity is not sufficient
95+
body.writeBytes(bytes)
96+
}
97+
}
98+
99+
extension PureSwiftJSONCoding.JSONDecoder: ContentDecoder {
100+
public func decode<D: Decodable>(
101+
_ decodable: D.Type,
102+
from body: ByteBuffer,
103+
headers: HTTPHeaders) throws -> D
104+
{
105+
guard headers.contentType == .json || headers.contentType == .jsonAPI else {
106+
throw Abort(.unsupportedMediaType)
107+
}
108+
var body = body
109+
return try self.decode(D.self, from: body.readBytes(length: body.readableBytes)!)
110+
}
111+
}
112+
```
113+
114+
Next, register the encoder and decoder for use in Vapor:
115+
116+
```swift
117+
let decoder = PureSwiftJSONCoding.JSONDecoder()
118+
ContentConfiguration.global.use(decoder: decoder, for: .json)
119+
120+
let encoder = PureSwiftJSONCoding.JSONEncoder()
121+
ContentConfiguration.global.use(encoder: encoder, for: .json)
122+
```
123+
124+
61125
## Performance
62126

63127
All tests have been run on a 2019 MacBook Pro (16" – 2,4 GHz 8-Core Intel Core i9). You can run the tests yourself

0 commit comments

Comments
 (0)