JSONEncoder/JSONDecoder and String convenience methods (#144)

Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
This commit is contained in:
Fabian Fett
2020-08-03 21:04:13 +02:00
committed by GitHub
co-authored by Konrad `ktoso` Malawski
parent 07a11c4c0f
commit 2067f4aea1
@@ -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<T: Encodable>(_ 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<T: Decodable>(_ type: T.Type, from string: String) throws -> T {
try self.decode(type, from: Data(string.utf8))
}
}