Created
March 29, 2020 18:27
-
-
Save appler1009/8a64b8e10a2d93d8ab293dac8a3abf64 to your computer and use it in GitHub Desktop.
A JSON version of CustomStringConvertible in Swift
This file contains hidden or 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
| import Foundation | |
| public protocol JSONStringConvertible: Codable, CustomStringConvertible { } | |
| extension JSONStringConvertible { | |
| public var description: String { | |
| let encoder = JSONEncoder() | |
| do { | |
| let data = try encoder.encode(self) | |
| return String(data: data, encoding: .utf8)! | |
| } catch { // EncodingError.invalidValue | |
| return "\(String(describing: type(of: self)))" | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So you can do something like: