Created
June 16, 2020 20:06
-
-
Save xmarkmaxwell/24b793965273b17ef191f79492fd98d8 to your computer and use it in GitHub Desktop.
Codable JSON extensions and Codec service.
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
// | |
// Codable+JSON.swift | |
// swytUI | |
// | |
// Created by Mark Maxwell on 12/11/19. | |
// Copyright © 2019 eonflux. All rights reserved. | |
// | |
import UIKit | |
import SwiftUI | |
typealias JSONString = String | |
extension Encodable { | |
func asJSONString() -> String { | |
let jsonEncoder = JSONEncoder() | |
let data = try? jsonEncoder.encode(self) | |
return String(data: data!, encoding: .utf8)! | |
} | |
} | |
extension Encodable { | |
func asJSONObject() -> [String: Any] { | |
do { | |
let data = asJSONString().data(using: .utf8)! | |
return try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any] | |
} catch let error as NSError { | |
assert(false,error.localizedDescription) | |
} | |
return [:] | |
} | |
} | |
class Codec { | |
static func json<T: Codable>(from object: T) -> JSONString? { | |
let jsonEncoder = JSONEncoder() | |
do { | |
let data = try jsonEncoder.encode(object) | |
return String(data: data, encoding: .utf8) | |
} catch { | |
assert(false,error.localizedDescription) | |
} | |
return nil | |
} | |
static func object<T: Codable>(fromJSON json: JSONString) -> T? { | |
let jsonDecoder = JSONDecoder() | |
let data = json.data(using: .utf8) | |
do { | |
return try jsonDecoder.decode(T.self, from: data!) | |
} catch { | |
assert(false, error.localizedDescription) | |
} | |
return nil | |
} | |
static func dictionary<T: Codable>(from object: T) -> [String: Any?] { | |
let data = try! JSONEncoder().encode(object) | |
let dictionary = try! JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] | |
return dictionary! | |
} | |
static func jsonObject(from string: JSONString) throws -> [String: Any] { | |
do { | |
let data = string.data(using: .utf8)! | |
return try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any] | |
} catch let error as NSError { | |
assert(false,error.localizedDescription) | |
} | |
return [:] | |
} | |
static func json(fromJSONObject object: AnyObject) -> String? { | |
do { | |
let data1 = try JSONSerialization.data(withJSONObject: object, options: JSONSerialization.WritingOptions.prettyPrinted) // first of all convert json to the data | |
return String(data: data1, encoding: String.Encoding.utf8) // the data will be converted to the string | |
} catch let error as NSError { | |
assert(false,error.localizedDescription) | |
} | |
return "" | |
} | |
} |
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
func store(item: Item) { | |
model.table.data = item.asJSONString() | |
} | |
func restore() -> Item { | |
if let stored = model.table.data ?? storage.string(forKey: tableKeyspace) { | |
return Codec.object(fromJSON: stored) ?? builder() | |
} | |
return builder() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment