Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JSONEncoder().stringEncode()
extension JSONEncoder {
func stringEncode<T>(_ value: T) throws -> String where T : Encodable {
guard let string = String(data: try self.encode(value), encoding: .utf8) else {
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Could not UTF-8 encode string"))
}
return string
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment