Gloss bindings for Alamofire for easy-peasy JSON serialization.
Add to your Podfile:
pod 'Alamofire-Gloss'
github "spxrogers/Alamofire-Gloss"
Alamofire-Gloss
lists Alamofire
and Gloss
as explicit Carthage dependencies, so it's only
necessary to list Alamofire-Gloss
in your Cartfile and it will pull down all
three libraries. Copy & Link generated frameworks as normal.
Create a Class
or Struct
which implements the Decodable
(or Glossy
) protocol.
import Foundation
import Gloss
struct Person: JSONDecodable {
let name: String
let age: Int?
init?(json: JSON) {
guard let name: String = "name" <~~ json
else { return nil }
self.name = name
self.age = "age" <~~ json
}
}
responseObject()
responseArray()
Alamofire.request(personUrl).responseObject(Person.self) { (response) in
switch response.result {
case .success(let person):
print("Found person: \(person)")
case .failure(let error):
print("Error'd: \(error)")
}
}
Alamofire.request(peopleUrl).responseArray(Person.self) { (response) in
switch response.result {
case .success(let people):
print("Found people: \(people)")
case .failure(let error):
print("Error'd: \(error)")
}
}
Issues and pull requests are welcome!
Steven Rogers @spxrogers
... to Harlan Kellaway for creating Gloss, my preferred JSON library :)
... to the Alamofire (+AFNetworking) team for paving the way of Networking in iOS.
Alamofire-Gloss is released under an MIT license. See LICENSE for more information.