Created
November 5, 2019 08:16
-
-
Save oleghnidets/cac648b42f7b4a5c870141d1d11c1236 to your computer and use it in GitHub Desktop.
Prin pretty JSON from Data
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 Data { | |
// NSString gives a nice sanitized debugDescription | |
var prettyPrintedJSONString: NSString? { | |
guard let object = try? JSONSerialization.jsonObject(with: self, options: []) else { | |
print("Cannot serialize JSON object.") | |
return nil | |
} | |
guard let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]) else { | |
print("Cannot serialize data object.") | |
return nil | |
} | |
guard let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { | |
print("Cannot get pretty string object.") | |
return nil | |
} | |
return prettyPrintedString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment