Skip to content

Typesave JSON parsing in Swift by using custom initializers

Notifications You must be signed in to change notification settings

lightsprint09/JSONParser

Repository files navigation

Build Status

#JSONParser in Swift Typesave JSON parsing in Swift by using custom initializers

##Example Your JSON payload

 { 
    "type": "ICE",
    "number": 102,
    "destination": "Berlin Hbf"
    "location": {   
          "longitude": 9.90280151,
          "latitude": 51.58730407
       }
 }

Your model type

 struct Train {
     let type: String
     let number: Int
     let destination: String
     let location: Location
 }
 
 struct Location {
     let longitude: Double
     let latitude: Double
 }

Import the module

import JSONParser

Extend the model type by implementing JSONParsable initializer

 extension Train: JSONParsable {
     init(JSON: ThrowableDictionary) throws {
         type = try JSON.valueFor("type")
         number = try JSON.valueFor("number")
         destination = try JSON.valueFor("destination")
         location = try JSON.valueFor(keyPath: "location")
     }
 }
let trainJSONData: NSData = //your json payload
let parser: JSONParsing = JSONParser()

do {
   let iceTrain: Train = try parser.parseObject(trainJSONData)
} catch {
   //handle error
}

##Installation Requires Swift 2/Xcode 7

Manually (iOS 7+)

  1. Copy the src folder into your Xcode project. (Make sure you add the files to your target(s))

Carthage

Add this line to your Cartfile:

github "lightsprint09/JSONParser" ~> 1.1.0 # Or latest version

Run carthage update.

##License MIT

About

Typesave JSON parsing in Swift by using custom initializers

Resources

Stars

Watchers

Forks

Packages

No packages published