This commit is contained in:
Suresh, Kamlesh 2019-11-04 17:24:04 -05:00
parent 205c83c097
commit 37e1cb9e36
3 changed files with 7 additions and 7 deletions

View File

@ -8,7 +8,7 @@ extension JSONDecoder: AnyDecoder {}
extension PropertyListDecoder: AnyDecoder {} extension PropertyListDecoder: AnyDecoder {}
extension Data { extension Data {
public func decodeToObject<T: Decodable>(using decoder: AnyDecoder = JSONDecoder()) throws -> T { public func decode<T: Decodable>(using decoder: AnyDecoder = JSONDecoder()) throws -> T {
return try decoder.decode(T.self, from: self) return try decoder.decode(T.self, from: self)
} }
} }
@ -27,7 +27,7 @@ extension KeyedDecodingContainerProtocol {
} }
extension Decodable { extension Decodable {
public static func decode(fileName: String, bundle: Bundle = Bundle.main) throws -> Self { public static func decode(fileName: String, bundle: Bundle = Bundle.main) throws -> Self{
guard let path = bundle.path(forResource: fileName, ofType: ".json") else { guard let path = bundle.path(forResource: fileName, ofType: ".json") else {
throw JSONError.pathNotFound throw JSONError.pathNotFound
} }
@ -37,7 +37,7 @@ extension Decodable {
} }
do { do {
return try jsonData.decode() return try jsonData.decode()
} catch { } catch {
throw JSONError.other(error: error) throw JSONError.other(error: error)
} }

View File

@ -8,7 +8,7 @@ extension JSONEncoder: AnyEncoder {}
extension PropertyListEncoder: AnyEncoder {} extension PropertyListEncoder: AnyEncoder {}
extension Encodable { extension Encodable {
public func encodeToData(using encoder: AnyEncoder = JSONEncoder()) throws -> Data { public func encode(using encoder: AnyEncoder = JSONEncoder()) throws -> Data {
return try encoder.encode(self) return try encoder.encode(self)
} }
@ -19,7 +19,7 @@ extension Encodable {
public func toJSONString() -> String? { public func toJSONString() -> String? {
guard let data = try? self.encode(), guard let data = try? self.encode(),
let string = String(data: data, encoding: .utf8) else { let string = String(data: data, encoding: .utf8) else{
return nil return nil
} }
return string return string

View File

@ -3,7 +3,7 @@ import Foundation
public typealias JSONValueArray = [[String: JSONValue]] public typealias JSONValueArray = [[String: JSONValue]]
public typealias JSONValueObject = [String: JSONValue] public typealias JSONValueObject = [String: JSONValue]
public enum JSONValueError: Error { public enum JSONValueError: Error{
case encode case encode
} }
@ -105,7 +105,7 @@ extension JSONValue: ExpressibleByDictionaryLiteral {
} }
extension Dictionary where Key == String, Value == Any { extension Dictionary where Key == String, Value == Any {
public func toJSONValue() throws -> [String:JSONValue] { public func toJSONValue() throws -> [String:JSONValue]{
let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted) let data = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return try JSONDecoder().decode([String:JSONValue].self, from: data) return try JSONDecoder().decode([String:JSONValue].self, from: data)
} }