MetovaJSONCodable
MetovaJSONCodable is a lightweight Codable protocol with addtional support for JSON
Requirements
- Swift 4.2
- iOS 9+
Installation
MetovaJSONCodable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'MetovaJSONCodable'
Usage
Any object that previously conformed to the Codable protocol can now use the JSONCodable protocol.
Object example
struct Token: JSONCodable {
var tokenID: Int
var email: String
var authenticationToken: String
}
JSON
let tokenJSON: JSON = [
"testID": 1,
"email": "[email protected]",
"authenticationToken": "token"
]
let token = Token(from: tokenJSON)
Custom Date Handling
This pod comes with several child protocols for handling json with custom date formats
Time interval since reference date
struct Token: JSONEpochDateCodable {
var tokenID: Int
var email: String
var authenticationToken: String
var expirationDate: Date
}
let tokenJSON: JSON = [
"tokenID": 1,
"email": "[email protected]",
"authenticationToken": "token",
"expirationDate": 1523560533
]
let token = Token(from: tokenJSON)
// token.expirationDate is a Date equaling 2018-04-12 19:15:33 +0000
Custom Date foramatter object
We also have a child protocol for supporting a custom date formatter. Here you will need to implement a dateFormatter computed variable that returns a date formatter in order to conform to the protocol
struct Token: JSONCustomDateFormatterCodable {
var testID: Int
var email: String
var expirationDate: Date
static var dateFormatter: DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
return dateFormatter
}
}
let validTestJSONISO8601: JSON = [
"testID": 2,
"email": "[email protected]",
"expirationDate": "2016-12-31T17:56:27.000Z"
]
let token = Token(from: tokenJSON)
// token.expirationDate is a Date equaling 2016-12-31 17:56:27 +0000
Custom Child Protocols
If you want to define your own date formatting strategy or some other permutation to the default coder/decoder, you simply need to make a child protocol and implement your own ecoder/coder
public protocol JSONDecodeableIOS8601: JSONDecodable {}
public extension JSONDecodeableIOS8601 {
static var jsonDecoder: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}
}
Documentation
Documentation can be found here.
Credits
MetovaJSONCodable is owned and maintained by Metova Inc.
License
MetovaJSONCodable is available under the MIT license. See the LICENSE file for more info.