/RxJSON

RxSwift wrapper for JSON

Primary LanguageSwiftMIT LicenseMIT

RxJSON

Swift CocoaPods Build Status codecov

RxSwift wrapper for JSON.

At a Glance

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)

Installation

Contributing

Any discussions and pull requests are welcomed 💖

To create a Xcode project:

$ swift package generate-xcodeproj

License

RxJSON is under MIT license. See the LICENSE file for more info.