CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.

JSONJam 1.0.0

JSONJam 1.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jul 2015
SPMSupports SPM

Maintained by Matt Luedke.



JSONJam 1.0.0

  • By
  • Matt Luedke

JSONJam

Single-line JSON serialization and deserialization in Swift is my jam.

Depends on JSONHelper and designed for use with Alamofire.

Requirements

  • iOS 8+
  • Swift 1.2+

Installation

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'

Usage

Defining the model class

In your Swift model class:

  • Import and subclass JSONJam
  • Define your model’s attributes
  • Override 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)
    }
}

Serialization

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)

Deserialization

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
                    }
                }
        }
    }

Example Project

To run the example project, clone the repo, and run pod install in the Example directory. It comes with tests! :]

Extras

Date Formats

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)
    }
}

Overriding serialization and deserialization

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)
                }
            }
        )
    }
}

Author

mluedke2, [email protected]

License

JSONJam is available under the MIT license. See the LICENSE file for more info.