From a7d842a37297340141f1726b0adf2605b7242d8d Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Thu, 23 Jul 2020 16:36:56 +0200 Subject: [PATCH 1/2] JSONEncoder/JSONDecoder and String convenience methods --- Sources/AWSLambdaRuntime/Lambda+Codable.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sources/AWSLambdaRuntime/Lambda+Codable.swift b/Sources/AWSLambdaRuntime/Lambda+Codable.swift index 6173b0d2..2aafd985 100644 --- a/Sources/AWSLambdaRuntime/Lambda+Codable.swift +++ b/Sources/AWSLambdaRuntime/Lambda+Codable.swift @@ -13,6 +13,7 @@ //===----------------------------------------------------------------------===// @_exported import AWSLambdaRuntimeCore +import struct Foundation.Data import class Foundation.JSONDecoder import class Foundation.JSONEncoder import NIO @@ -130,3 +131,17 @@ extension JSONEncoder: LambdaCodableEncoder { return buffer } } + +extension JSONEncoder { + /// Convenience method to allow encoding json directly into a String. It can be used to encode a payload into an APIGateway.V2.Response's body. + public func encodeAsString(_ value: T) throws -> String { + try String(decoding: self.encode(value), as: Unicode.UTF8.self) + } +} + +extension JSONDecoder { + /// Convenience method to allow decoding json directly from a String. It can be used to decode a payload from an APIGateway.V2.Request's body. + public func decode(_ type: T.Type, from string: String) throws -> T { + try self.decode(type, from: Data(string.utf8)) + } +} From 9d28bfe1be554f660b7f51fa7a12f5308abf34c1 Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Tue, 28 Jul 2020 14:55:03 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Konrad `ktoso` Malawski --- Sources/AWSLambdaRuntime/Lambda+Codable.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/AWSLambdaRuntime/Lambda+Codable.swift b/Sources/AWSLambdaRuntime/Lambda+Codable.swift index 2aafd985..495e5a69 100644 --- a/Sources/AWSLambdaRuntime/Lambda+Codable.swift +++ b/Sources/AWSLambdaRuntime/Lambda+Codable.swift @@ -133,14 +133,14 @@ extension JSONEncoder: LambdaCodableEncoder { } extension JSONEncoder { - /// Convenience method to allow encoding json directly into a String. It can be used to encode a payload into an APIGateway.V2.Response's body. + /// Convenience method to allow encoding json directly into a `String`. It can be used to encode a payload into an `APIGateway.V2.Response`'s body. public func encodeAsString(_ value: T) throws -> String { try String(decoding: self.encode(value), as: Unicode.UTF8.self) } } extension JSONDecoder { - /// Convenience method to allow decoding json directly from a String. It can be used to decode a payload from an APIGateway.V2.Request's body. + /// Convenience method to allow decoding json directly from a `String`. It can be used to decode a payload from an `APIGateway.V2.Request`'s body. public func decode(_ type: T.Type, from string: String) throws -> T { try self.decode(type, from: Data(string.utf8)) }