changes
This commit is contained in:
parent
205c83c097
commit
37e1cb9e36
@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user