Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
/ jsonjam Public archive
forked from mluedke2/jsonjam

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

License

Notifications You must be signed in to change notification settings

Pluto-tv/jsonjam

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod "JSONJam", '~> 1.0.0'

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 69.6%
  • Shell 27.0%
  • Ruby 1.7%
  • Objective-C 1.7%