aerogear-ios-jsonsz
NOTE: The library has been tested with Xcode 6.3
Serialize ‘Swift’ objects back-forth from their JSON representation the 'easy way’.
Project Info | |
---|---|
License: | Apache License, Version 2.0 |
Build: | Cocoapods |
Documentation: | https://aerogear.org/docs/guides/aerogear-ios-2.X/ |
Issue tracker: | https://issues.jboss.org/browse/AGIOS |
Mailing lists: | aerogear-users (subscribe) |
aerogear-dev (subscribe) |
Example usage
Here is a typical example usage:
class Contact : JSONSerializable {
var first: String?
var last: String?
var addr: Address?
required init() {}
class func map(source: JsonSZ, object: Contact) {
object.first <= source["first"]
object.last <= source["last"]
object.addr <= source["addr"]
}
}
class Address: JSONSerializable {
var street: String?
var poBox: Int?
var city: String?
var country: String?
var arr:[String]
required init() {}
class func map(source: JsonSZ, object: Address) {
object.street <= source["street"]
object.poBox <= source["poBox"]
object.city <= source["city"]
object.country <= source["country"]
}
}
// construct object
let address = Address()
address.street = "Street"
address.poBox = 100
address.city="New York"
address.country = "US"
let user = User()
user.first = "John"
user.last = "Doe"
// assign Address to User
user.addr = address
// initialize serializer
let serializer = JsonSZ()
// serialize ToJSON
let JSON = serializer.toJSON(user)
// ..send json to server
// serialize fromJSON
let user = serializer.fromJSON(JSON, to:User.self)
// user now should be initialized
println(user.first!)
...
As long as your model object implements the JSONSerializable
protocol and its required methods of init
and map
, the JsonSZ
class should be able to serialize and deserialize your objects to JSON. The library supports all the primitive types including arrays and dictionaries plus relationship between the objects (as shown by the User, Address example above). Head over to our unit tests for more examples usage.
Give it a go and let us know if it does help you on your projects!
Last, we would like to give appreciation and credit to the existing serialization libraries of ObjectMapper and SwiftMapper for giving us an initial bootstrap and ideas to base our development of this library. Thank you guys!
Adding the library to your project
To add the library in your project, you can either use Cocoapods or manual install in your project. See the respective sections below for instructions:
Using Cocoapods
Support for Swift frameworks is supported from CocoaPods-0.36 release upwards. In your Podfile
add:
pod 'AeroGearJsonSZ'
and then:
pod install
to install your dependencies
Manual Installation
Follow these steps to add the library in your Swift project:
- Add AeroGearJsonSZ as a submodule in your project. Open a terminal and navigate to your project directory. Then enter:
bash git submodule add https://github.com/aerogear/aerogear-ios-jsonsz.git
- Open the
aerogear-ios-jsonsz
folder, and drag theAeroGearJsonSZ.xcodeproj
into the file navigator in Xcode. - In Xcode select your application target and under the
Targets
heading section, ensure that the 'iOS Deployment Target’ matches the application target of AeroGearJsonSZ.framework (Currently set to 8.0). - Select the
Build Phases
heading section, expand theTarget Dependencies
group and addAeroGearJsonSZ.framework
. - Click on the
+
button at the top left of the panel and selectNew Copy Files Phase
. Rename this new phase toCopy Frameworks
, set theDestination
toFrameworks
, and addAeroGearJsonSZ.framework
.
Documentation
For more details about the current release, please consult our documentation.
Development
If you would like to help develop AeroGear you can join our developer’s mailing list, join #aerogear on Freenode, or shout at us on Twitter @aerogears.
Also takes some time and skim the contributor guide
Questions?
Join our user mailing list for any questions or help! We really hope you enjoy app development with AeroGear!
Found a bug?
If you found a bug please create a ticket for us on Jira with some steps to reproduce it.