RxSwift wrapper for JSON.
This is an example of converting a json dictionary observable to a string observable:
URLSession.shared.rx.json(url: "https://api.github.com/repos/ReactorKit/ReactorKit")
.mapJSON("owner") // Observable<Any> -> Observable<Any>
.mapJSON("login", String.self) // Observable<Any> -> Observable<String>
.bind(to: ownerNameLabel.rx.text)
mapJSON()
supports both JSON dictionary and array:
// Dictionary
Observable<Any>.mapJSON("key") // Observable<Any>
Observable<Any>.mapJSON("key", Int.self) // Observable<Int>
// Array
Observable<Any>.mapJSON(at: 2) // Observable<Any>
Observable<Any>.mapJSON(at: 3, String.self) // Observable<String>
mapJSON()
will throw a RxJSONError
when there's no value for given accessor or fails to cast to a given type:
// Dictionary
source.mapJSON("unknownKey") // Event.error(RxJSONError.valueNotFound)
source.mapJSON("name", Int.key) // Event.error(RxJSONError.castingFailed)
// Array
source.mapJSON(at: -1) // Event.error(RxJSONError.valueNotFound)
source.mapJSON(at: 0, Int.key) // Event.error(RxJSONError.castingFailed)
-
Using CocoaPods:
pod 'RxJSON'
-
Using Carthage:
This is not supported yet. See Carthage#1945 for details.
Any discussions and pull requests are welcomed 💖
To create a Xcode project:
$ swift package generate-xcodeproj
RxJSON is under MIT license. See the LICENSE file for more info.