Latest 1.1.0
Homepage https://github.com/DroidsOnRoids/Moya-JASON
License MIT
Platforms ios 8.0, osx 10.10, watchos 2.0, tvos 9.0, requires ARC
Authors

============

CocoaPods

JASON bindings for
Moya for easier JSON serialization with RxSwift and ReactiveCocoa bindings.

Installation

CocoaPods

pod 'Moya-JASON', '1.0.0'

The subspec if you want to use the bindings over RxSwift.

pod 'Moya-JASON/RxSwift', '1.0.0'

And the subspec if you want to use the bindings over ReactiveCocoa.

pod 'Moya-JASON/ReactiveCocoa', '1.0.0'

Usage

Create a model struct or class. It needs to implement protocol Mappable.

Example without handling errors while mapping

import JASON
import Moya_JASON

private extension JSONKeys {
    static let id       = JSONKey<Int>("id")
    static let language = JSONKey<String>("language")
    static let url      = JSONKey<String?>("url")
}

struct Repository: Mappable {

    let identifier: Int
    let language: String
    let url: String?

    init(_ json: JSON) throws {
        identifier  = json[.id]
        language    = json[.language]
        url         = json[.url]
    }
}

Example with handling mapping errors

import JASON
import Moya_JASON

public enum UserParsingError: Error {
    case login
}

private extension JSONKeys {
    static let login = JSONKey<String>("login")
}

struct User: Mappable {

    let login: String

    init(_ json: JSON) throws {
        login = json[.login]
        if login.isEmpty {
            throw UserParsingError.login
        }
    }
}

Then you have methods that extends the response from Moya. These methods are:

mapObject()
mapObject(withKeyPath:)
mapArray()
mapArray(withKeyPath:)

While using mapObject() tries to map whole response data to object,
with mapObject(withKeyPath:) you can specify nested object in a response to
fetch. For example mapObject(withKeyPath: "owner"). RxSwift and ReactiveCocoa extensions have all those methods as well.

1. Normal usage (without RxSwift or ReactiveCocoa)

provider = MoyaProvider<GitHub>()
provider.request(GitHub.repos("mjacko")) { (result) in
    if case .success(let response) = result {
        do {
            let repos: [Repository] = try response.mapArray()
            print(repos)
        } catch {
            print("There was something wrong with the mapping!")
        }
    }
}

2. RxSwift

provider = RxMoyaProvider<GitHub>()
provider
    .request(GitHub.repo("Moya/Moya"))
    .mapObject(User.self, keyPath: "owner")
    .subscribe { event in
        switch event {
        case .next(let user):
            print(user)
        case .error(let error):
            print(error)
        default: break
        }
}

3. ReactiveCocoa

provider = ReactiveCocoaMoyaProvider<GitHub>()
provider
    .request(GitHub.repos("mjacko"))
    .mapArray(Repository.self)
    .observeOn(UIScheduler())
    .start { event in
        switch event {
        case .value(let repos):
            print(repos)
        case .failed(let error):
            print(error)
        default: break
        }
}

Author

Sunshinejr, [email protected], @thesunshinejr

License

Moya-JASON is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Moya-JASON",
    "version": "1.1.0",
    "summary": "JASON bindings for Moya.",
    "description": "[JASON](https://github.com/delba/JASON) bindings forn[Moya](https://github.com/Moya/Moya) for easier JSON serialization.nIncludes [RxSwift](https://github.com/ReactiveX/RxSwift/) and [ReactiveCocoa](https://github.com/ReactiveCocoa/ReactiveCocoa) bindings as well.nInstructions on how to use it are inn[the README](https://github.com/DroidsOnRoids/Moya-JASON).",
    "homepage": "https://github.com/DroidsOnRoids/Moya-JASON",
    "license": "MIT",
    "authors": {
        "u0141ukasz Mru00f3z": "[email protected]"
    },
    "source": {
        "git": "https://github.com/DroidsOnRoids/Moya-JASON.git",
        "tag": "1.1.0"
    },
    "social_media_url": "https://twitter.com/thesunshinejr",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "requires_arc": true,
    "default_subspecs": "Core",
    "pushed_with_swift_version": "3.0",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Source/*.swift",
            "dependencies": {
                "Moya": [
                    "~> 8.0.0"
                ],
                "JASON": []
            },
            "frameworks": "Foundation"
        },
        {
            "name": "RxSwift",
            "source_files": "Source/RxSwift/*.swift",
            "dependencies": {
                "Moya/RxSwift": [
                    "~> 8.0.0"
                ],
                "Moya-JASON/Core": [],
                "RxSwift": []
            }
        },
        {
            "name": "ReactiveCocoa",
            "source_files": "Source/ReactiveCocoa/*.swift",
            "dependencies": {
                "Moya/ReactiveCocoa": [
                    "~> 8.0.0"
                ],
                "Moya-JASON/Core": [],
                "ReactiveSwift": []
            }
        }
    ]
}

Pin It on Pinterest

Share This