-
-
Save liweiz/5196bbd0b993dd7520b859d533c3e1d3 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