Skip to content

Instantly share code, notes, and snippets.

@griotspeak
Created October 2, 2016 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save griotspeak/dc41f9c2a84d50592690d01c0043aa1e to your computer and use it in GitHub Desktop.
Save griotspeak/dc41f9c2a84d50592690d01c0043aa1e to your computer and use it in GitHub Desktop.
Convenience overload for JSON parsing
import Foundation
extension JSONSerialization {
enum CastingError : Swift.Error {
case incompatibleType
}
static func jsonObject<T>(with data: Data, options: JSONSerialization.ReadingOptions) throws -> T {
let anyResult: Any = try jsonObject(with: data, options: options)
if let result = anyResult as? T {
return result
} else {
throw CastingError.incompatibleType
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment