Latest 1.0.0
License MIT
Platforms ios 8.0, requires ARC

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

Depends on JSONHelper and designed for use with Alamofire.


  • iOS 8+
  • Swift 1.2+


To integrate JSONJam into your Xcode project using CocoaPods, specify it in your Podfile:

source ''
platform :ios, '8.0'

pod "JSONJam", '~> 1.0.0'


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)


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

// plural

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):
                case .None:
                    var customObjects: [CustomObject]?
                    customObjects <-- JSON
                    if let customObjects = 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! :]


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

Latest podspec

    "name": "JSONJam",
    "version": "1.0.0",
    "summary": "JSON serialization and deserialization in Swift is my jam.",
    "homepage": "",
    "license": "MIT",
    "authors": {
        "Matt Luedke": "[email protected]"
    "source": {
        "git": "",
        "tag": "1.0.0"
    "platforms": {
        "ios": "8.0"
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*",
    "resource_bundles": {
        "JSONJam": [

Pin It on Pinterest

Share This