Code Monkey home page Code Monkey logo

reactivemapper's Introduction

ReactiveMapper

Build Status Carthage compatible Swift 3.0 Platform

A collection of reactive JSON parsing helpers for the Mapper JSON parser.

Usage

ReactiveSwift

ReactiveMapper supports JSON mapping for ReactiveSwift on values in a Signal or SignalProducer stream.

// Create models from a JSON dictionary
let jsonSignalProducer: SignalProducer<Any, NSError> = // ...
jsonSignalProducer.mapToType(User).startWithResult { result in
    // use the decoded User model
    let user: User? = result.value
}

// Create array of models from array of JSON dictionaries
let jsonSignalProducer: SignalProducer<Any, NSError> = // ...
jsonSignalProducer.mapToTypeArray(Task).startWithResult { result in
    // use the decoded array of Task models
    let tasks: [Task]? = result.value
}

Model → JSON encoding

Mapper only supports decoding JSON to models, but not the other way around. ReactiveMapper introduces a simple protocol Encodable that models may adopt in order to encode themselves into a JSON representation.

struct Dog {
    let name: String
}

extension Dog: Encodeable {
    func encode() -> [String: Any] {
        return ["name": name]
    }
}

struct User {
    let id: String
    let name: String?
    let dog: Dog
}

extension User: Encodeable {
    func encode() -> [String: Any] {
        return [
            "id": id,
            "name": name ?? Encodeable.null,
            "dog": dog.encode()
        ]
    }
}

let dog = Dog(name: "Waldo")
let user = User(id: "1", name: nil, dog: dog)
let encoded = user.encode() // ["id": "1", "name": NSNull, "dog": ["name": "Waldo"]]

Version Compatibility

Current Swift compatibility breakdown:

Swift Version Framework Version
3.x 1.x

Installation

Carthage

Add the following line to your Cartfile.

github "aschuch/ReactiveMapper”

Then run carthage update.

Manually

Just drag and drop the three .swift files in the ReactiveMapper folder into your project.

Tests

Open the Xcode project and press ⌘-U to run the tests.

Alternatively, all tests can be run from the terminal using xcodebuild.

xcodebuild \
  -project Example.xcodeproj \
  -scheme ReactiveMapper \
  -sdk iphonesimulator \
  -destination 'platform=iOS Simulator,name=iPhone 6,OS=10.1' \
  test

Contributing

  • Create something awesome, make the code better, add some functionality, whatever (this is the hardest part).
  • Fork it
  • Create new branch to make your changes
  • Commit all your changes to your branch
  • Submit a pull request

Contact

Feel free to get in touch.

reactivemapper's People

Contributors

aschuch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.