View on GitHub

ObjectMapper

JSON Object mapping written in Swift

Download this project as a .zip file Download this project as a tar.gz file

ObjectMapper is a framework written in Swift that makes it easy for you to convert your Model objects to and from JSON.

Features:

To support mapping, a class just needs to implement the MapperProtocol. ObjectMapper uses the "<=" operator to define how each member variable maps to and from JSON.

class User: MapperProtocol {

    var username: String?
    var age: Int?
    var weight: Double?
    var arr: [AnyObject]?
    var dict: [String : AnyObject] = [:]
    var friend: User?
    var birthday: NSDate?

    // MapperProtocol    
    class func map(mapper: Mapper, object: User) {
        object.username <= mapper["username"]
        object.age <= mapper["age"]
        object.weight <= mapper["weight"]
        object.arr <= mapper["arr"]
        object.dict <= mapper["dict"]
        object.friend <= mapper["friend"]
        object.birthday <= (mapper["birthday"], DateTransform<NSDate, Int>())
    }
}

Once your class implements MapperProtocol, the Mapper class handles everything else for you:

Convert a JSON string to a model object:

let user = Mapper().map(JSONString, to: User.self)

Convert a model object to a JSON string:

let JSONString = Mapper().toJSONString(user)

Object mapper can handle classes composed of the following types:

ObjectMapper also supports Transforms that convert values during the mapping process. To use a transform, simply create a tuple with the mapper["field_name"] and the transform of choice on the right side of the '<=' operator:

object.birthday <= (mapper["birthday"], DateTransform<NSDate, Int>())

The above transform will convert the JSON Int value to an NSDate when reading JSON and will convert the NSDate to an Int when converting objects to JSON.

You can easily create your own custom transforms by subclassing and overriding the methods in the MapperTransform class:

public class MapperTransform<ObjectType, JSONType> {
    init(){

    }

    func transformFromJSON(value: AnyObject?) -> ObjectType? {
        return nil
    }

    func transformToJSON(value: ObjectType?) -> JSONType? {
        return nil
    }
}