Skip to content

Instantly share code, notes, and snippets.

@oleghnidets
Created November 5, 2019 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleghnidets/cac648b42f7b4a5c870141d1d11c1236 to your computer and use it in GitHub Desktop.
Save oleghnidets/cac648b42f7b4a5c870141d1d11c1236 to your computer and use it in GitHub Desktop.
Prin pretty JSON from Data
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