Giter Site home page Giter Site logo

arrow's Introduction

Arrow ๐Ÿน

Language: Swift 2 Platform: iOS 8+ Carthage compatible Build Status codebeat badge License: MIT

Elegant JSON Parsing in Swift

identifier <-- json["id"]
name <-- json["name"]
stats <== json["stats"]

Before

if let id = json["id"] as? Int {
  identifier = id
}
if let n = json["name"] as? String {
  name = n
}
if let s = Stats(json:json) {
  stats = s
}

After

identifier <-- json["id"]
name <-- json["name"]
stats <== json["stats"]

๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰

Why

Because parsing JSON in Swift is full of unecessary if lets, obvious casts and nil-checks
There must be a better way

How

By using a simple arrow operator that takes care of the boilerplate code for us.
Json mapping code becomes concise and maintainable โค๏ธ

What

  • Simple & Lightweight (~50lines)
  • Pure Swift
  • Leaves your models clean
  • Implicitly casts JSON values to the right types in your model
  • Does not crash if JSON key is not there, nor returns nil, it simply doesn't do anything
  • NSDate Parsing
  • No overly complex obscure functional chaining operator overloading voodoo magic ?==:>>><> ๐Ÿ˜…

Installation

Using Carthage

github "s4cha/Arrow"

Manually

Simply Copy and Paste Arrow.swift in your Xcode Project :) https://github.com/s4cha/Arrow/blob/master/Arrow.swift

As A Framework

Grab this repository and build the Framework target on the example project. Then Link against this framework.

Show me the code

Swift Model

struct Profile {
    var identifier = 0
    var name = ""
    var stats = Stats()
}

JSON File

{
    "id": 15678,
    "name": "John Doe",
    "stats": {
        "numberOfFriends": 163,
        "numberOfFans": 10987
    }
}

Usual Swift JSON Parsing (Chaos)

var profile = Profile()
if let id = json["id"] as? Int {
    profile.identifier = id
}  
if let name = json["name"] as? String {
    profile.name = name
}
if let statsJson = json["stats"] as? AnyObject {
    if let numberOfFans = statsJson["numberOfFans"] as? Int {
        profile.stats.numberOfFans = numberOfFans
    }
    if let numberOfFriends = statsJson["numberOfFriends"] as? Int {
        profile.stats.numberOfFriends = numberOfFriends
    }
}

With Arrow --> Sanity preserved

extension Profile:ArrowParsable {
    init(json: JSON) {
        identifier <-- json["id"]
        name <-- json["name"]
        stats <== json["stats"]
    }
}

Integration

  • Step 1 - Copy paste Arrow.swift in your Xcode Project
  • Step 2 - Create you model parsing extension like so : "Profile+Arrow.swift"
// Profile+Arrow.swift
extension Profile:ArrowParsable {
    init(json: JSON) {
        identifier <-- json["id"]
        name <-- json["name"]
        stats <== json["stats"]
    }
}
  • Step 3 - Use it:
let profile = Profile(json: json)
  • Step 4 - Ther is no step 4

How Does that work

  • <-- Arrow Operator is for all Swift Types : Int.. Double .. String .. NSDate etc
  • <== Thick Arrow Operator is for your own custom models

Notice earlier we typed :

stats <== json["stats"]

That's because we created and extension "Stats+Arrow.swift" enabling us to use the thick Arrow Operator

//  Stats+Arrow.swift

import Foundation

extension Stats:ArrowParsable {
    init(json: JSON) {
        numberOfFriends <-- json["numberOfFriends"]
        numberOfFans <-- json["numberOfFans"]
    }
}

Flexible you said

  • DO I have to use the <== for my sub models
  • Nope, you could write it like so if you wanted :
stats.numberOfFriends <-- json.valueForKeyPath("stats.numberOfFriends")
stats.numberOfFans <-- json.valueForKeyPath("stats.numberOfFans")
  • Hey I don't want to parse NSDates in evry files, do you have something for me?

Sure, just set your date format once and you're done.

// Configure NSDate Parsing
Arrow.dateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ")

Acknoledgments

This wouldn't exist without YannickDot (https://github.com/YannickDot) and Damien-nd (https://github.com/damien-nd) <3

Other repos โค๏ธ

Arrow is part of a series of lightweight libraries aiming to make developing iOS Apps a breeze :

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.