Skip to content

Instantly share code, notes, and snippets.

@hellensoloviy
Created March 13, 2019 08:06
Show Gist options
  • Save hellensoloviy/9cceacd15cfe165151be62a92d3485d6 to your computer and use it in GitHub Desktop.
Save hellensoloviy/9cceacd15cfe165151be62a92d3485d6 to your computer and use it in GitHub Desktop.
JSONDecodable
import Foundation
protocol JSONDecodable {
func jsonParams() throws -> [String : Any]
}
extension JSONDecodable where Self : Codable {
func jsonParams() throws -> [String : Any] {
let encoder = JSONEncoder()
let data = try encoder.encode(self)
return try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any] //force unwrap
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment