Skip to content

An Alamofire extension which converts JSON response data into swift objects using EVReflection

License

Notifications You must be signed in to change notification settings

tamir7/AlamofireJsonToObjects

 
 

Repository files navigation

AlamofireJsonToObjects

Build Status Issues Documentation Stars

Version Language Platform Support License

Git Twitter ![LinkedIn](https://img.shields.io/badge/linkedin-Edwin Vermeer-blue.svg?style=flat) Website [eMail](mailto:[email protected]?SUBJECT=About AlamofireJsonToObjects)

With AlamofireJsonToObjects it's extremely easy to fetch a json feed and parse it into objects. No property mapping is required. Reflection is used to put the values in the corresponding properties.

AlamofireJsonToObjects is based on the folowing libraries:

  • Alamofire is an elegant HTTP Networking library in Swift
  • EVReflection is used to parse the JSON result to your objects

This library was greatly inspired by AlamofireObjectMapper

Run the tests to see AlamofireJsonToObjects in action.

Using AlamofireJsonToObjects in your own App

'AlamofireJsonToObjects' is available through the dependency manager CocoaPods. You do have to use cocoapods version 0.36 or later

You can just add AlamofireJsonToObjects to your workspace by adding the folowing 2 lines to your Podfile:

use_frameworks!
pod "AlamofireJsonToObjects"

I have now moved on to Swift 2. If you want to use AlamofireJsonToObjects, then get that version by using the podfile command:

use_frameworks!
pod "AlamofireJsonToObjects", '~> 1.0'

Version 0.36 of cocoapods will make a dynamic framework of all the pods that you use. Because of that it's only supported in iOS 8.0 or later. When using a framework, you also have to add an import at the top of your swift file like this:

import AlamofireJsonToObjects

If you want support for older versions than iOS 8.0, then you can also just copy the AlamofireJsonToObjects.swift to your app.

Sample code

class WeatherResponse: EVObject {
    var location: String?
    var three_day_forecast: [Forecast] = [Forecast]()
}

class Forecast: EVObject {
    var day: String?
    var temperature: NSNumber?
    var conditions: String?
}

class AlamofireJsonToObjectsTests: XCTestCase {
    func testResponseObject() {
        let URL = "https://raw.githubusercontent.com/evermeer/AlamofireJsonToObjects/master/AlamofireJsonToObjectsTests/sample_json"
        Alamofire.request(.GET, URL)
           .responseObject { (response: Result<WeatherResponse, NSError>) in
           if let result = response.value {
            // That was all... You now have a WeatherResponse object with data
           }
        }
        waitForExpectationsWithTimeout(10, handler: { (error: NSError!) -> Void in
            XCTAssertNil(error, "\(error)")
        })
    }
}

The code above will pass the folowing json to the objects:

{  "location": "Toronto, Canada",    
   "three_day_forecast": [
      { "conditions": "Partly cloudy",
        "day" : "Monday",
        "temperature": 20 
      }, { 
        "conditions": "Showers",
        "day" : "Tuesday",
        "temperature": 22 
      }, { 
        "conditions": "Sunny",
        "day" : "Wednesday",
        "temperature": 28 
      }
   ]
}

License

AlamofireJsonToObjects is available under the MIT 3 license. See the LICENSE file for more info.

My other libraries:

Also see my other open source iOS libraries:

  • EVReflection - Swift library with reflection functions with support for NSCoding, Printable, Hashable, Equatable and JSON
  • EVCloudKitDao - Simplified access to Apple's CloudKit
  • EVFaceTracker - Calculate the distance and angle of your device with regards to your face in order to simulate a 3D effect
  • EVURLCache - a NSURLCache subclass for handling all web requests that use NSURLReques
  • AlamofireJsonToObject - An Alamofire extension which converts JSON response data into swift objects using EVReflection
  • AlamofireXmlToObject - An Alamofire extension which converts XML response data into swift objects using EVReflection and XMLDictionary
  • AlamofireOauth2 - A swift implementation of OAuth2 using Alamofire
  • EVWordPressAPI - Swift Implementation of the WordPress (Jetpack) API using AlamofireOauth2, AlomofireJsonToObjects and EVReflection (work in progress)
  • PassportScanner - Scan the MRZ code of a passport and extract the firstname, lastname, passport number, nationality, date of birth, expiration date and personal numer.

About

An Alamofire extension which converts JSON response data into swift objects using EVReflection

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 73.8%
  • Ruby 23.3%
  • Objective-C 2.9%