Skip to content

Commit 316f138

Browse files
committed
Reuse JSONCoders
1 parent ac29098 commit 316f138

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Sources/AWSLambdaRuntime/Lambda+Codable.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,30 @@ internal struct CodableVoidLambdaClosureWrapper<In: Decodable>: LambdaHandler {
8888
public extension EventLoopLambdaHandler where In: Decodable, Out: Encodable {
8989
func encode(allocator: ByteBufferAllocator, value: Out) throws -> ByteBuffer? {
9090
// nio will resize the buffer if necessary
91-
// FIXME: reusable JSONEncoder and buffer
9291
var buffer = allocator.buffer(capacity: 1024)
93-
try JSONEncoder().encode(value, into: &buffer)
92+
try Lambda.defaultJSONEncoder.encode(value, into: &buffer)
9493
return buffer
9594
}
9695

9796
func decode(buffer: ByteBuffer) throws -> In {
98-
// FIXME: reusable JSONDecoder
99-
try JSONDecoder().decode(In.self, from: buffer)
97+
try Lambda.defaultJSONDecoder.decode(In.self, from: buffer)
10098
}
10199
}
102100

101+
private extension Lambda {
102+
/// the default json encoder used in `EventLoopLambdaHandler` if Out == Encodable
103+
static let defaultJSONEncoder = JSONEncoder()
104+
105+
/// the default json decoder used in `EventLoopLambdaHandler` if In == Decodable
106+
static let defaultJSONDecoder = JSONDecoder()
107+
}
108+
103109
public extension EventLoopLambdaHandler where In: Decodable, Out == Void {
104110
func encode(allocator: ByteBufferAllocator, value: Void) throws -> ByteBuffer? {
105111
nil
106112
}
107113

108114
func decode(buffer: ByteBuffer) throws -> In {
109-
// FIXME: reusable JSONDecoder
110-
try JSONDecoder().decode(In.self, from: buffer)
115+
try Lambda.defaultJSONDecoder.decode(In.self, from: buffer)
111116
}
112117
}

0 commit comments

Comments
 (0)