From 4af67950f48db0f6afaab450d3d18a0b831c98c0 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Fri, 23 Mar 2018 21:03:40 -0400 Subject: [PATCH 1/2] data coders --- Sources/Core/DataCoders.swift | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Sources/Core/DataCoders.swift diff --git a/Sources/Core/DataCoders.swift b/Sources/Core/DataCoders.swift new file mode 100644 index 00000000..033e062a --- /dev/null +++ b/Sources/Core/DataCoders.swift @@ -0,0 +1,65 @@ +/// A type capable of decoding `Decodable` types from `Data`. +/// +/// print(data) /// Data +/// let user = try JSONDecoder().decode(User.self, from: data) +/// print(user) /// User +/// +public protocol DataDecoder { + /// Decodes an instance of the supplied `Decodable` type from `Data`. + /// + /// print(data) /// Data + /// let user = try JSONDecoder().decode(User.self, from: data) + /// print(user) /// User + /// + /// - parameters: + /// - decodable: Generic `Decodable` type (`D`) to decode. + /// - from: `Data` to decode a `D` from. + /// - returns: An instance of the `Decodable` type (`D`). + /// - throws: Any error that may occur while attempting to decode the specified type. + func decode(_ decodable: D.Type, from data: Data) throws -> D where D: Decodable +} + +extension DataDecoder { + /// Convenience method for decoding a `Decodable` type from something `LosslessDataConvertible`. + /// + /// + /// print(data) /// LosslessDataConvertible + /// let user = try JSONDecoder().decode(User.self, from: data) + /// print(user) /// User + /// + /// - parameters: + /// - decodable: Generic `Decodable` type (`D`) to decode. + /// - from: `LosslessDataConvertible` to decode a `D` from. + /// - returns: An instance of the `Decodable` type (`D`). + /// - throws: Any error that may occur while attempting to decode the specified type. + public func decode(_ decodable: D.Type, from data: LosslessDataConvertible) throws -> D where D: Decodable { + return try decode(D.self, from: data.convertToData()) + } +} + +/// A type capable of encoding `Encodable` objects to `Data`. +/// +/// print(user) /// User +/// let data = try JSONEncoder().encode(user) +/// print(data) /// Data +/// +public protocol DataEncoder { + /// Encodes the supplied `Encodable` object to `Data`. + /// + /// print(user) /// User + /// let data = try JSONEncoder().encode(user) + /// print(data) /// Data + /// + /// - parameters: + /// - encodable: Generic `Encodable` object (`E`) to encode. + /// - returns: Encoded `Data` + /// - throws: Any error taht may occur while attempting to encode the specified type. + func encode(_ encodable: E) throws -> Data where E: Encodable +} + +/// MARK: Default Conformances + +extension JSONDecoder: DataDecoder { } +extension JSONEncoder: DataEncoder { } +extension PropertyListDecoder: DataDecoder { } +extension PropertyListEncoder: DataEncoder { } From 3762c6083036d381edad2f59ad67cee0a9087159 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Sat, 24 Mar 2018 17:04:53 -0400 Subject: [PATCH 2/2] remove plist coders --- Sources/Core/DataCoders.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/Sources/Core/DataCoders.swift b/Sources/Core/DataCoders.swift index 033e062a..c84f0bde 100644 --- a/Sources/Core/DataCoders.swift +++ b/Sources/Core/DataCoders.swift @@ -61,5 +61,3 @@ public protocol DataEncoder { extension JSONDecoder: DataDecoder { } extension JSONEncoder: DataEncoder { } -extension PropertyListDecoder: DataDecoder { } -extension PropertyListEncoder: DataEncoder { }