CICOAutoCodable is an extension for codable, a new feature in Swift 4. It is very simple to achieve mutual conversion between model and JSON. And it provides automatic code completion using sourcery.
You can simply add CICOAutoCodable to your Podfile
:
pod "CICOAutoCodable"
You can simply add CICOAutoCodable to your Cartfile
:
github "cicout/cico_auto_codable"
enum MyEnum: String, CICOAutoCodable {
case one
case two
}
class MyClass: CICOAutoCodable {
private(set) var stringValue: String?
private(set) var dateValue: Date?
private(set) var intValue: Int = 0
private(set) var doubleValue: Double = 1.0
private(set) var boolValue: Bool = false
private(set) var enumValue: MyEnum = .one
private(set) var urlValue: URL?
private(set) var nextValue: MyClass?
private(set) var arrayValue: [String]?
private(set) var dicValue: [String: String]?
}
{
"stringValue": "string",
"dateValue": 1234567890123,
"intValue": 123,
"doubleValue": 2.5,
"boolValue": true,
"enumValue": "two",
"urlValue": "https://www.google.com",
"nextValue": {
"stringValue": "string",
"intValue": 123,
"doubleValue": 2.5,
"boolValue": true,
"enumValue": "two"
},
"arrayValue": [
"string0",
"string1",
],
"dicValue": {
"key0": "value0",
"key1": "value1"
}
}
- Default JSONDecoder
let object = MyClass.init(jsonString: myJSONString)
- Custom JSONDecoder
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .millisecondsSince1970
let object = MyClass.init(jsonString: myJSONString, jsonDecoder: decoder)
- Default JSONEncoder
let jsonString = object?.toJSONString()
- Custom JSONEncoder
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .millisecondsSince1970
let jsonString = object?.toJSONString(jsonEncoder: encoder)
NSCodingSerializable can make NSCoding Class in OBJ-C conform to Codable protocol in Swift.
@interface OCTestClass : NSObject <NSCoding>
@property (nonatomic, strong) NSString *text;
@end
@implementation OCTestClass
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super init];
if (self) {
self.text = [coder decodeObjectForKey:@"text"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.text forKey:@"text"];
}
@end
extension OCTestClass: NSCodingSerializable {}
let objectValue = OCTestClass()
let objectValueWrapper = SerializableWrapper.init(value: objectValue)
let objectValueJSONString = objectValueWrapper.toJSONString()
It can make integer enum in OBJ-C conform to Codable protocol in Swift.
NS_ENUM(NSInteger, OCTestIntEnum) {
one = 1,
two
};
extension OCTestIntEnum: Codable {}
You don't need to write any mapping code when there is no custom mapping relationship using codable. However, you need to manually define the CodingKeys enumeration and list all the mappings, including the part that does not require a custom mapping, when there is any custom mapping relationship. CICOAutoCodable can complete the code for you automaticaly using sourcery.
- Copy "sourcery" directory in this framework source into your project;
- Get the latest sourcery by CocoaPod; (replace
{YourProjectDir}
with your real project directory)
cd "{YourProjectDir}"/sourcery/source
pod update
- Open "yourProjectTarget" -> "Build Phases" -> "+" -> "New Run Script Phase", and add new run script below: (replace
{YourProjectName}
with your real project name)
if [ "${CONFIGURATION}" = "Debug" ]; then
echo "[***** Start Running CICOAutoCodable Script *****]"
./sourcery/source/Pods/Sourcery/bin/sourcery --sources ./Carthage/Checkouts/cico_auto_codable/CICOAutoCodable --sources ./"{YourProjectName}" --templates ./Carthage/Checkouts/cico_auto_codable/sourcery/templates/ --output ./sourcery/auto_generated
echo "[***** End Running CICOAutoCodable Script *****]"
fi
- Define your model and run;
enum MyEnum: String, CICOAutoCodable {
case one
case two
}
class MyClass: CICOAutoCodable {
private(set) var stringValue: String?
private(set) var dateValue: Date?
private(set) var intValue: Int = 0
private(set) var doubleValue: Double = 1.0
private(set) var boolValue: Bool = false
private(set) var enumValue: MyEnum = .one
private(set) var urlValue: URL?
private(set) var next: MyClass?
private(set) var arrayValue: [String]?
private(set) var dicValue: [String: String]?
private(set) var ignoredValue: String?
}
- CodingKeys Definition
extension MyClass {
enum CICOCustomCodingKeys: String {
case next = "nextValue"
}
enum CICOIgnoredCodingKeys: String {
case ignoredValue
}
}
- Auto Generated Code
// sourcery:inline:auto:MyClass.CICOAutoCodable_Auto_Generated_CodingKeys_Head
enum CodingKeys: String, CodingKey {
// sourcery:inline:auto:MyClass.CodingKeys.CICOAutoCodable_Auto_Generated_Custom_CodingKeys
case next = "nextValue"
// sourcery:inline:auto:MyClass.CodingKeys.CICOAutoCodable_Auto_Generated_CodingKeys
case stringValue
case dateValue
case intValue
case doubleValue
case boolValue
case enumValue
case urlValue
case arrayValue
case dicValue
// sourcery:inline:auto:MyClass..CICOAutoCodable_Auto_Generated_CodingKeys_Tail
}
// sourcery:end
- CodingKeys Definition
extension MyClass {
enum CICOIgnoredCodingKeys: String {
case ignoredValue
}
enum CodingKeys: String, CodingKey {
case next = "nextValue"
}
}
- Auto Generated Code
extension MyClass {
enum CICOIgnoredCodingKeys: String {
case ignoredValue
}
enum CodingKeys: String, CodingKey {
case next = "nextValue"
// sourcery:inline:auto:MyClass.CodingKeys.CICOAutoCodable_Auto_Generated_CodingKeys
case stringValue
case dateValue
case intValue
case doubleValue
case boolValue
case enumValue
case urlValue
case arrayValue
case dicValue
// sourcery:end
}
}
- iOS 12.0+
- Swift 5.0+
CICOAutoCodable is released under the MIT license. See LICENSE for details.
Have a question? Please open an issue!