CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.
TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jul 2015 |
SPMSupports SPM | ✗ |
Maintained by Matt Luedke.
Single-line JSON serialization and deserialization in Swift is my jam.
Depends on JSONHelper and designed for use with Alamofire.
JSONJam is available through CocoaPods. To install it, simply add the following line to your Podfile:
platform :ios, '8.0'
use_frameworks!
pod "JSONJam", :git => 'https://github.com/mluedke2/jsonjam.git'
In your Swift model class:
JSONJam
propertyMap()
and map your JSON keys to the relevant parameter (pass the parameter as a reference)Example:
import JSONJam
public class User: JSONJam {
public var name: String?
public var favoriteNumber: Int?
override public func propertyMap() {
map("name", string: &name)
map("favorite_number", int: &favoriteNumber)
}
}
You can prepare your custom objects to be passed as a JSON like this:
// singular
customObject.parameterize()
// plural
customObjects.parameterize()
An example in Alamofire:
Alamofire.request(.POST, baseURL + "/customObjects", parameters: customObject.parameterize(), encoding: .JSON)
You can turn JSON objects and arrays into your custom objects like this:
// singular
var customObject: CustomObject?
customObject <-- JSON
// plural
var customObjects: [CustomObject]?
customObjects <-- JSON
An Alamofire example (within a sample networking controller):
func getCustomObjects(success:([CustomObject]) -> Void, fail:NetworkError) -> Void {
Alamofire.request(.GET, baseURL + "/customObjects")
.responseJSON {(request, response, JSON, error) in
switch error {
case .Some(let error):
fail(error)
case .None:
var customObjects: [CustomObject]?
customObjects <-- JSON
if let customObjects = customObjects {
success(customObjects)
} else {
// JSON parsing error
}
}
}
}
To run the example project, clone the repo, and run pod install
in the Example
directory. It comes with tests! :]
Supply a date format string along with your NSDate
(or arrays thereof), and it will be used in serialization/deserialization:
import JSONJam
class MyObject: JSONJam {
var dateFormat = "yyyy-MM-dd HH:mm:ss"
var creationDate: NSDate?
var transactionDates: [NSDate]?
override func propertyMap() {
map("creation_date", date: &creationDate, dateFormat: dateFormat)
map("transaction_dates", dateArray: &transactionDates, dateFormat: dateFormat)
}
}
You can override the serialization/deserialization for a specific parameter and provide closures to do the job yourself.
This is useful in the case of enums, like in ShoeSize
in the Example Project:
import JSONJam
class ShoeSize: JSONJam {
enum SizeSystem: String {
case UnitedStates = "US"
case Europe = "EUR"
case UnitedKingdom = "UK"
case Australia = "AUS"
}
var size: Int?
var sizeSystem: SizeSystem?
override func propertyMap() {
map("size", int: &size)
map("system",
serializeClosure:{ (inout outgoingParameter: AnyObject?) -> Void in
outgoingParameter = self.sizeSystem?.rawValue as AnyObject?
},
deserializeClosure: { (data: String?) -> Void in
if let data = data {
self.sizeSystem = SizeSystem(rawValue: data)
}
}
)
}
}
mluedke2, [email protected]
JSONJam is available under the MIT license. See the LICENSE file for more info.