CodableExtensions is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'CodableExtensions'
and run
pod install
To make a custom decoding transformer, you just need to implement DecodingContainerTransformer
An example is provided in the project for converting a Regex
import CodableExtensions
public class RegexCodableTransformer: DecodingContainerTransformer {
public typealias Input = String
public typealias Output = NSRegularExpression
public init() {}
public func transform(_ decoded: Input) throws -> Output {
return try NSRegularExpression(pattern: decoded, options: [])
}
}
The Input
and Output
types need defining to match the type you expect to be converting from and to, in this case the input is a String
and the output is an NSRegularExpression
.
The logic to perfrom the transformation is then performed in the transform
function. If there's an error at any point then that needs to be thrown.
The custom transformer can then be used in the Decodable
initialiser
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
regex = try container.decode(.regex, transformer: RegexCodableTransformer())
}
To make a custom encoding transformer, make a class implementing EncodingContainerTransformer
Example encoding an NSRegularExpression
class RegexCodableTransformer: EncodingContainerTransformer {
typealias Input = String
typealias Output = NSRegularExpression
public func transform(_ encoded: Output) throws -> Input {
return encoded.pattern
}
}
and to use this transformer implement the Encodable function encode(to encoder: Encoder)
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(regex, forKey: .regex, transformer: RegexCodableTransformer())
}
If your transformer conforms to both the EncodingContainerTransformer
and DecodingContainerTransformer
you can use the CodingContainerTransformer
typealias
For full examples check out the tests https://github.com/jamesruston/CodableExtensions/blob/master/Example/Tests/Tests.swift
For a writeup on the motivation behind this library check my Medium article out https://medium.com/@James_Ruston/codable-vs-objectmapper-af5fe8e8efd5
CodableExtensions is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'CodableExtensions'
Run pod install
to integrate CodableExtensions
with your workspace.
CodableExtensions is available through Carthage. To install it, simply add the following line to your Carthage file:
github "jamesruston/CodableExtensions"
Run carthage update
to build the framework and drag the built CodableExtensions.framework
into your Xcode project.
https://medium.com/@James_Ruston/
CodableExtensions is available under the MIT license. See the LICENSE file for more info.