A collection of reactive JSON parsing helpers for the Mapper JSON parser.
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
}
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"]]
Current Swift compatibility breakdown:
Swift Version | Framework Version |
---|---|
3.x | 1.x |
Add the following line to your Cartfile.
github "aschuch/ReactiveMapper”
Then run carthage update
.
Just drag and drop the three .swift
files in the ReactiveMapper
folder into your project.
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
- 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
Feel free to get in touch.
- Website: https://schuch.me
- Twitter: @schuchalexander