Giter VIP home page Giter VIP logo

guardedswiftyjson's Introduction

GuardedSwiftyJSON

Why should I use this?

This library makes initializing models with JSON data with SwiftyJSON a lot easier.

Often with SwiftyJSON I end up doing something like this:

import SwiftyJSON

struct Model {
  let name : String
  let height : Double

  init?(json: JSON) {
    guard let name = json["name"].string, let height = json["height"].double else {
      return nil
    }

    self.name = name
    self.height = height
  }
}

which gets annoying when you have more than two or three properties you want to guard your model on.

Example

GuardedSwiftyJSON solves this by providing an initializer which will fail the initialization if properties that you request are not present.

import GuardedSwiftyJSON

struct Model : JsonInitializable {
  let name : String
  let height : Double

  init(json: GuardedJSON) {
    name = json["name"].string
    height = json["height"].double
  }
}

And then your object will get an initializer that allows it to be created from a Swifty JSON object:

let data : JSON = ["name": "Arthur Swiftington", "height": 182.8]

let model : Model? = Model(json: data)

If either one of name or height are not present, the initialization will fail.

You can specify optional properties by using the optional prefix:

import GuardedSwiftyJSON

struct Model : JsonInitializable {
  let name : String
  let height : Double?

  init(json: GuardedJSON) {
    name = json["name"].string
    height = json["height"].optionalDouble
  }
}

Then, if those optional properties do not exist, they will not cause initialization to abort.

GuardedSwiftyJSON provies the following protocol

protocol JsonInitializable {
  init?(json: JSON)
  init(json: GuardedJSON)
}

and a default implementation of init?(json: JSON) which automatically calls the proxying initializer and then fails the initialization if any of the required JSON properties are not present.

Installation

Carthage

github "wiggzz/GuardedSwiftyJSON"

Cocoapods

pod 'GuardedSwiftyJSON'

Contributing

Pull requests and issues are welcomed.

guardedswiftyjson's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.