Last active
September 19, 2016 18:21
-
-
Save dlo/7701240b4cae1094bb6f3a574aaedf0e to your computer and use it in GitHub Desktop.
JSONSerialization extension to hone in on which values are invalid in complex Swift arrays or dictionaries.
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 JSONSerialization { | |
static func invalidJSONValue(data: Any) -> AnyObject? { | |
if let elements = data as? [Any] { | |
if !isValidJSONObject(data) { | |
for element in elements { | |
if let value = invalidJSONValue(data: element) { | |
return value | |
} | |
} | |
for element in elements { | |
if !isValidJSONObject([element]) { | |
return element as AnyObject | |
} | |
} | |
return elements as AnyObject | |
} | |
} else if let elements = data as? [String: Any], | |
let value = invalidJSONValue(data: Array(elements.values)) { | |
return value as AnyObject | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment