Created
October 2, 2016 03:30
-
-
Save griotspeak/dc41f9c2a84d50592690d01c0043aa1e to your computer and use it in GitHub Desktop.
Convenience overload for JSON parsing
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
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