Aiakos is an iOS framework for transformation and (de)serialization between JSON and model objects
- Automatic (de)serialization between model objects and JSON data with custom options.
- Automatic archive/unarchive of model objects.
- iOS 9.1+
- Xcode 7.1+
- Currently I only test the framework on iOS, but porting to OSX is really easy (just copy the code, I believe it will work :)
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Alamofire into your Xcode project using Carthage, specify it in your Cartfile
:
github "zetasq/Aiakos"
Run carthage
to build the framework and drag the built Aiakos.framework
into your Xcode project.
If you prefer not to use Carthage, you can integrate Aiakos into your project manually.
Due to Swift 2.1 reflection API's poor function, you need to make properties non-optional, otherwise Aiakos could not find the unwrapped type of your optional properties. So just make your properties non-optional and give each of them an initial value.
Just make your model object inherit from AiaModel, that's it :)
All these property types are allowed: String(NSString), NSNumber, AiaModel, [String], [NSNumber], [AiaModel], [String: String], [String: NSNumber], [String: AiaModel].If you use property of type [AiaModel] or [String: AiaModel], be sure to override modelContainerPropertyAnnotation in your model type.
import Aiakos
class MyModel: AiaModel {
var args: [String: String] = [:]
var origin: NSString = ""
var version: NSNumber = 0
var subModels: [MySubModel] = []
var modelDic: [String: MySubModel] = [:]
override class var modelContainerPropertyAnnotation: [String: AiaModelContainerPropertyType]? {
return [
"subModels": .ArrayOfModel(MySubModel.self),
"modelDic": .DictionaryOfModel(MySubModel.self)
]
}
}
If you want to make a custom mapping between property names and JSON keys, conform your model to AiaJSONCustomPropertyMapping. Only properties whose name returned by customPropertyMapping will be (de)serialized from/to JSON data.
import Aiakos
class MySubModel: AiaModel, AiaJSONCustomPropertyMapping {
var contentLength: String = ""
var acceptedLanguage: String = ""
var acceptedEncoding: String = ""
var xForwardedPort: String = ""
var xForwardedFor: String = ""
static var customPropertyMapping: [String: String] {
return [
"contentLength": "Content-Length",
"acceptedLanguage": "Accept-Language",
"acceptedEncoding": "Accept-Encoding",
"xForwardedPort": "X-Forwarded-Port",
"xForwardedFor": "X-Forwarded-For",
]
}
}
(De)serialize JSON by using AiaConverter's class methods (for detailed list of methods, check AiaConverter's API):
import Aiakos
func test() {
Alamofire.request(.GET, "https://api.github.com/users/mralexgray/repos")
.response { (request, response, data, error) -> Void in
do {
let models = try AiaConverter.modelArrayOfType(MyModel.self, fromJSONArrayData: data!) // deserializing
let recreatedData = try AiaConverter.jsonArrayDataFromModelArray(models) // serializing
} catch {
print(error)
}
}
}
Aiakos doesn't automatically persist your objects for you. However, AiaModel
does conform to <NSCoding>
, so model objects can be archived to disk using
NSKeyedArchiver
. Caution: Here AiaJSONCustomPropertyMapping is also used to map property names.