Skip to content

Instantly share code, notes, and snippets.

@joshuajhomann
Created September 19, 2021 16:56
Show Gist options
  • Save joshuajhomann/d8706fd05a47986b9ba3bd5233c098a8 to your computer and use it in GitHub Desktop.
Save joshuajhomann/d8706fd05a47986b9ba3bd5233c098a8 to your computer and use it in GitHub Desktop.
struct A: Codable {
var a = "test"
var b = 1
}
private extension Encodable {
var prettyPrinted: String {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
return (try? encoder.encode(self))
.flatMap { String(data: $0, encoding: .utf8) } ?? "\(self)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment