Skip to content

Instantly share code, notes, and snippets.

@ramakser
Last active January 30, 2017 10:13
Show Gist options
  • Save ramakser/4a7ca57996df06ef39ba08cbb5b9823e to your computer and use it in GitHub Desktop.
Save ramakser/4a7ca57996df06ef39ba08cbb5b9823e to your computer and use it in GitHub Desktop.
func JSONStringify(value: [String: AnyHashable],prettyPrinted:Bool = false) -> String{
let options = prettyPrinted ? JSONSerialization.WritingOptions.prettyPrinted : JSONSerialization.WritingOptions(rawValue: 0)
if JSONSerialization.isValidJSONObject(value) {
do {
let data = try JSONSerialization.data(withJSONObject: value, options: options)
if let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue) {
return string as String
}
} catch {
print("error")
}
}
return ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment