Skip to content

Instantly share code, notes, and snippets.

View s4cha's full-sized avatar
🎯
Focusing

Sacha DSO s4cha

🎯
Focusing
View GitHub Profile
typealias JSON = AnyObject
protocol JSONParsable {
init(json: JSON)
}
extension Profile:JSONParsable {
init(json: JSON) {
if let id:Int = json["id"] as? Int {
identifier = id
}
if let n:String = json["name"] as? String {
name = n
}
}
}
struct Profile {
var identifier = 0
var name = ""
}
infix operator <-- {}
func <-- <T>(inout left: T, right: AnyObject?) {
if let v: T = right as? T {
left = v
}
}
extension Profile:JSONParsable {
init(json: JSON) {
identifier <-- json["id"]
name <-- json["name"]
}
}
func <-- <T>(inout left: T?, right: AnyObject?) {
if let v: T = right as? T {
left = v
}
}
infix operator <== {}
func <== <T:JSONParsable>(inout left:T, right: AnyObject?) {
if let r: AnyObject = right {
left = T.self(json:r)
}
}
extension Stats:JSONParsable {
init(json: JSON) {
numberOfFollowers <-- json["numberOfFollowers"]
numberOfFollowing <-- json["numberOfFollowing"]
}
}
extension Profile:JSONParsable {
init(json: JSON) {
identifier <-- json["id"]
name <-- json["name"]
stats <== json["stats"]
}
}
{
"id": 15678,
"name": "John Doe",
"stats": {
"numberOfFollowers": 163,
"numberOfFollowing": 10987
}
}