@@ -35,17 +35,17 @@ Add `pure-swift-json` as dependency to your `Package.swift`:
35
35
36
36
``` swift
37
37
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 " )),
39
39
],
40
40
```
41
41
42
42
Add ` PureSwiftJSONCoding ` to the target you want to use it in.
43
43
44
44
``` swift
45
45
targets: [
46
- .target (
47
- name : " MyFancyTarget " ,
48
- dependencies : [ " PureSwiftJSONCoding " ]),
46
+ .target (name : " MyFancyTarget " , dependencies : [
47
+ . product ( name : " PureSwiftJSONCoding " , package : " pure-swift-json " ) ,
48
+ ])
49
49
]
50
50
```
51
51
@@ -58,6 +58,70 @@ let bytesArray = try JSONEncoder().encode(myEncodable)
58
58
let myDecodable = try JSONDecoder ().decode (MyDecodable.self , from : bytes)
59
59
```
60
60
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
+
61
125
## Performance
62
126
63
127
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