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 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"
}
}
let object = MyClass.init(jsonString: myJSONString)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .millisecondsSince1970
let object = MyClass.init(jsonString: myJSONString, jsonDecoder: decoder)
let jsonString = object?.toJSONString()
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .millisecondsSince1970
let jsonString = object?.toJSONString(jsonEncoder: encoder)
@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
class SwiftTestClass: CICOAutoCodable {
var objectValue: OCCodingObjectWrapper<OCTestClass>?
}
NS_ENUM(NSInteger, OCTestIntEnum) {
one = 1,
two
};
class SwiftTestClass: CICOAutoCodable {
var enumValue: OCEnumWrapper<OCTestIntEnum>?
}
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.
{YourProjectDir}
with your real project directory)cd "{YourProjectDir}"/sourcery/source
pod update
{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
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?
}
extension MyClass {
enum CICOCustomCodingKeys: String {
case next = "nextValue"
}
enum CICOIgnoredCodingKeys: String {
case ignoredValue
}
}
// 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
extension MyClass {
enum CICOIgnoredCodingKeys: String {
case ignoredValue
}
enum CodingKeys: String, CodingKey {
case next = "nextValue"
}
}
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
}
}
CICOAutoCodable is released under the MIT license. See LICENSE for details.
Have a question? Please open an issue!
link |
Stars: 3 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics