Skip to content

ReactiveSwift JSON parsing helpers for lyft/mapper. — Edit

License

Notifications You must be signed in to change notification settings

mbuchetics/ReactiveMapper

 
 

Repository files navigation

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.

About

ReactiveSwift JSON parsing helpers for lyft/mapper. — Edit

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 96.9%
  • Objective-C 3.1%