Created
July 20, 2018 00:50
-
-
Save rnapier/a5e8b412d0b268fae02e8db9b6f122a8 to your computer and use it in GitHub Desktop.
JSONEncoder().stringEncode()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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