Swift JSON Parsing directly into object structure

Parsing JSON in Objective-C was always repetitive work and not straightforward at all.
With Swift and the work of David Owens (https://github.com/owensd/json-swift) I was able to implement a pretty straightforward JSON to Object mapper.

As basis I am gonna use this very small JSON Example:

{
 "employees": [
 {
 "firstName": "John", 
 "lastName": "Doe",
 "age" : 26
 }, 
 
 {
 "firstName" : "Anna", 
 "lastName":"Smith",
 "age" : 30
 },
 {
 "firstName":"Peter", 
 "lastName":"Jones",
 "age" : 45
 }
]}

Next step is to define the object model. I am gonna create two classes: EmployeeContainer and the Employee class.
The EmployeeContainer will contain as Array Employee objects.

The definition is looking like that:

EmployeeContainer.swift

class EmployeeContainer : ROJSONObject {
    required init() {
        super.init();
    }

    required init(jsonData:AnyObject) {
        super.init(jsonData: jsonData)
    }

    lazy var employees:[Employee] = {
        return Value<[Employee]>.getArray(self, key: "employees") as [Employee]
    }()
}

Employee.swift

class Employee : ROJSONObject {

    required init() {
        super.init();
    }
    
    required init(jsonData:AnyObject) {
        super.init(jsonData: jsonData)
    }
    
    var firstname:String {
        return Value.get(self, key: "firstName")
    }
    
    var lastname:String {
        return Value.get(self, key: "lastName")            
    }
    
    var age:Int {
        return Value.get(self, key: "age")
    }
}

So basically this is the object model and directly the mapping of the properties in the JSON file to the properties in the Swift class.
The following line is using my generic method to fetch the JSON value from the JSON Dictionary which was already prepared by the Libary of
David Owens.

Every JSON supported Datatype can be access directly over the generic Value class.

return Value.get(self, key: "firstName")

Then I have create a BaseWebservice class which is asynchronously loading my JSON file and return it in the callback. I am not gonna show
the code of this BaseWebservice class. But you can find it on my git account: https://github.com/prine

The call of the Webservice and actual converting into the Dataobject is done here (in the ViewController):

 
var baseWebservice:BaseWebservice = BaseWebservice();
var urlToJSON = "http://prine.ch/employees.json"

var callbackJSON = {(status:Int, jsonResponse:AnyObject!) -> () in
    var employeeContainer:EmployeeContainer = EmployeeContainer(jsonData: jsonResponse)
    
    println(employeeContainer.employees[0].firstname)
    println(employeeContainer.employees[1].firstname)
    
    for employee in employeeContainer.employees {
        println("Firstname: \(employee.firstname) Lastname: \(employee.lastname) age: \(employee.age)");
    }
}

baseWebservice.get(urlToJSON, callback:callbackJSON)

Which provides the following output:

John
Anna
Firstname: John Lastname: Doe age: 26
Firstname: Anna Lastname: Smith age: 30
Firstname: Peter Lastname: Jones age: 45

The whole code can be found on github under the following link:
https://github.com/prine/ROJSONParser

Advertisements