Giter VIP home page Giter VIP logo

alexander's Introduction

#Alexander

Build Status Carthage Compatible CocoaPods Compatible

Alexander is an extremely simple JSON helper written in Swift. It brings type safety and Foundation helpers to the cumbersome task of JSON unpacking.

Requirements

Xcode Swift iOS tvOS OS X
7.3 2.2 8.0 9.0 10.9

Installation

github "hodinkee/alexander"

#####CocoaPods

pod 'Alexander'

Usage

DecoderType

Make a new DecoderType that can unpack your object.

struct User {
    var ID: String
    var name: String
    var email: String
}

struct UserDecoder: DecoderType {
    typealias Value = User
    static func decode(JSON: Alexander.JSON) -> Value? {
        guard
            let ID = JSON["id"]?.stringValue,
            let name = JSON["name"]?.stringValue,
            let email = JSON["email"]?.stringValue
        else {
            return nil
        }
        return User(ID: ID, name: name, email: email)
    }
}

Now you can do let author = JSON["user"]?.decode(UserDecoder) to get a single user, or let users = JSON["users"]?.decodeArray(UserDecoder) to get an array of users.

You can make DecodableTypes for all kinds of things.

struct SizeDecoder {
    typealias Value = CGSize
    static func decode(JSON: Alexander.JSON) -> Value? {
        guard
            let width = JSON["width"]?.doubleValue,
            let height = JSON["height"]?.doubleValue
        else {
            return nil
        }
        return CGSize(width: width, height: height)
    }
}

Alexander ships with a handful of decoders for common types:

  • NSDateTimeIntervalSince1970Decoder
  • NSDateTimeIntervalSinceReferenceDateDecoder
  • NSURLDecoder
  • RawRepresentableDecoder

Nested Objects

Most of Alexander's power comes from its two subscript operators: subscript[key: String] -> JSON? and subscript[index: Int] -> JSON?. These operators allow you to unpack nested objects without having to refer to each intermediate step by hand. Something like let nextCursor = JSON["meta"]?["pagination"]?["next_cursor"]?.stringValue is a single line of code.

Enums & RawRepresentable

You can also decode anything that conforms to the RawRepresentable type. For example, assume the following enum:

enum Planet: String {
    case Mercury = "mercury"
    case Venus = "venus"
    case Earth = "earth"
    case Mars = "mars"
    case Jupiter = "jupiter"
    case Saturn = "saturn"
    case Uranus = "uranus"
    case Neptune = "neptune"
    // case Pluto = "pluto" =(
}

Because Planet is backed by a String raw value type, it is inheriently RawRepresentable. This means you can do let planet = JSON["planet"]?.decode(RawRepresentableDecoder<Planet>) or let planets = JSON["planets"]?.decodeArray(RawRepresentableDecoder<Planet>).

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.