Guest User

Untitled

a guest
Apr 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. extension Encodable {
  2. func toJsonObject<T>() -> T? {
  3. guard let data = try? JSONEncoder().encode(self),
  4. let dict = (try? JSONSerialization.jsonObject(with: data, options: [])) as? T else { return nil }
  5.  
  6. return dict
  7. }
  8. }
  9.  
  10. extension Decodable {
  11. static func fromJsonObject(object: Any) -> Self? {
  12. guard let data = try? JSONSerialization.data(withJSONObject: object, options: []),
  13. let decoded = try? JSONDecoder().decode(self, from: data) else { return nil }
  14.  
  15. return decoded
  16. }
  17. }
Add Comment
Please, Sign In to add comment