Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension Encodable {
- func toJsonObject<T>() -> T? {
- guard let data = try? JSONEncoder().encode(self),
- let dict = (try? JSONSerialization.jsonObject(with: data, options: [])) as? T else { return nil }
- return dict
- }
- }
- extension Decodable {
- static func fromJsonObject(object: Any) -> Self? {
- guard let data = try? JSONSerialization.data(withJSONObject: object, options: []),
- let decoded = try? JSONDecoder().decode(self, from: data) else { return nil }
- return decoded
- }
- }
Add Comment
Please, Sign In to add comment