Jsonable is a Swift protocol that makes it easy to represent your custom objects as a dictionary.
Tired of writing zip()
, toDict()
, and other such methods for every one of your models? Then Jsonable is for you! Simply have your custom object conform to Jsonable, and with a simple json()
call, it will be converted into a dictionary.
Install
Jsonable can be installed via Cocoapods. Add the following line into your podfile:
pod Jsonable
Example Usage
import Jsonable
class User: Jsonable {
var name: String
var address: String?
init(name: String, address: String) {
self.name = name
self.address = address
}
}
// Elsewhere in the code
let user = User(name: "Rey", address: "Jakku")
let params = user.json()
// params now contains: ["name": "Rey", "address": "Jakku"]
Jsonable can even handle nested objects like so:
import Jsonable
class Job: Jsonable {
var name: String
var shouldUnderscore = true
init(name: String) {
self.name = name
}
}
class User: Jsonable {
var name: String
var address: String?
var job: Job
init(name: String, address: String, job: Job) {
self.name = name
self.address = address
self.job = job
}
}
// Elsewhere in the code
let job = Job(name: "Apple")
let user = User(name: "Awesome Dev", address: "Cupertino", job: job)
let params = user.json() // params now contains: ["name": "Awesome Dev", "address": "Cupertino", "job": ["name": "Apple"]]