JSONParserSwift Framework, JSON Parser for Swift


Server sends the all JSON data in black and white format i.e. its all strings & we make hard efforts to typecast them into their respective datatypes as per our model class.

Now, there’s comes JSONParserSwift framework between the server data and our code to magically converts those strings into the required respective datatypes as per our model classes without writing any code.

Installation

JSONParserSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "JSONParserSwift"

Implementation

To parse any JSON String or Dictionary to your model you have to create a class and subclass it by ParsableModel. Now you will need to create the properties in the model class. You can create these properties with same name or different name as keys in json string. If you declare properties with same name as key in json then you need to declare only properties. But if you want to have different names for properties and keys then you need to conform protocol JSONKeyCoder and implement method func key(for key: String) -> String?

Example

If you have to parse following JSON String:

{
"responseStatus": {
"statusCode": 101,
"message": "Error Message"
},
"responseData": {
"employeeId": 1002,
"employeeName": "Demo Employee",
"employeeEmail": "abc@def.com",
"employeeDepartment": "IT"
}
}

You will need to create models as follows:

class BaseResponse: ParsableModel {
var responseStatus: ResponseStatus?
var responseData: Employee?
}
class ResponseStatus: ParsableModel {
var statusCode: NSNumber?
var message: String?
}
class Employee: ParsableModel {
var employeeId: NSNumber?
var employeeName: String?
var employeeEmail: String?
var employeeDepartment: String?
}

Now to parse the JSON you just need to call following method:

do {
let baseResponse: BaseResponse = try JSONParserSwift.parse(string: jsonString)
// Use base response object here
} catch {
print(error)
}

The model can have reference to other model’s which are subclass of ParsableModel or it can have Array of models.

Get JSON String from Object

To get the JSON string from an object you just have to call JSONParserSwift.getJSON(object: NSObject) method to get the JSON.

If you want to have different keys and properties name then Conform to protocol JSONKeyCoder and implement its method key(for key: String) -> String? as given below:

class TestModel: JSONKeyCoder {
    var test: String?
var number: Double = 0
var boolValue: Bool = false
var anotherTest: TestModel?
var array: [TestModel]?

public func key(for key: String) -> String? {
switch key {
case "boolValue": // Properties name
return "bool_value" // Key in response
case "anotherTest":
return "another_key"
default:
return nil
}
}
}

If you want to convert the model object into JSON string then call method getJSON as given below:

// Prepare Test Model
let testModel: TestModel = TestModel()
testModel.test = "xyz"
testModel.number = 10.0
testModel.boolValue = true

let anotherTestModel = TestModel()
anotherTestModel.test = "abc"
anotherTestModel.number = 23
anotherTestModel.boolValue = false

testModel.anotherTest = anotherTestModel
testModel.array = [anotherTestModel]
do {
// Convert into json string
let jsonString = try JSONParserSwift.getJSON(object: testModel)
print("Json String : \(jsonString)")
} catch {
print(error)
}

The JSON string for the above code will be:

{
"bool_value":true,
"number":10,
"test":"xyz",
"array":[
{
"bool_value":false,
"number":23,
"test":"abc",
"array": null,
"another_key": null
}
],
"another_key": {
"bool_value":false,
"number":23,
"test":"abc",
"array": null,
"another_key": null
}
}

Note: Currently this version do not support Optionals with Int and Array of Optional types. So prefer to use NSNumber for number related datas

GitHub Link: https://github.com/mukeshydv/JSONParserSwift

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.