GithubHelp home page GithubHelp logo

moya-swiftyjsonmapper's Introduction

Moya-SwiftyJSONMapper

Version Build Status Language License Platform Twitter

Usage

Example project

To run the example project, clone the repo, and run pod install from the Example directory first. It includes sample code and unit tests.

Model definitions

Create a Class or Struct which implements the Mappable protocol.

import Foundation
import Moya_SwiftyJSONMapper
import SwiftyJSON

final class GetResponse : ALSwiftyJSONAble {
    
    let url:URL?
    let origin:String
    let args:[String: String]?
    
    required init?(jsonData:JSON){
        self.url = jsonData["url"].URL
        self.origin = jsonData["origin"].stringValue
        self.args = jsonData["args"].object as? [String : String]
    }
    
}

1. Without RxSwift or ReactiveCocoa

stubbedProvider.request(ExampleAPI.GetObject) { (result) -> () in
    switch result {
    case let .success(response):
        do {
            let getResponseObject = try response.map(to: GetResponse.self)
            print(getResponseObject)
        } catch {
            print(error)
        }
    case let .failure(error):
        print(error)
    }
}

2. With ReactiveCocoa

stubbedProvider.reactive.request(token: ExampleAPI.GetObject).map(to: GetResponse.self).on(failed: { (error) -> () in
    print(error)
}) { (response) -> () in
    print(response)
}.start()

3. With RxSwift

let disposeBag = DisposeBag()
stubbedProvider.rx.request(ExampleAPI.GetObject).map(to: GetResponse.self).subscribe(onNext: { (response) -> Void in
    print(response)
}, onError: { (error) -> Void in
    print(error)
}).addDisposableTo(disposeBag)

Installation

CocoaPods

Moya-SwiftyJSONMapper is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "Moya-SwiftyJSONMapper"

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

pod "Moya-SwiftyJSONMapper/RxSwift"

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

pod "Moya-SwiftyJSONMapper/ReactiveCocoa"

Other repo's

If you're using JASON for parsing JSON data, checkout Moya-JASONMapper.

If you're really into reactive programming, checkout ALDataRequestView and handle those edge cases with ease!

Author

Antoine van der Lee

Mail: [email protected]
Home: www.avanderlee.com
Twitter: @twannl

License

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

moya-swiftyjsonmapper's People

Contributors

akshay23 avatar albert-sf avatar avdlee avatar basthomas avatar edouardouvrard avatar iida-hayato avatar sandeepbol avatar sandpat avatar seekrtech-cyhsu avatar systembugtj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

moya-swiftyjsonmapper's Issues

Carthage support

Will be a Carthage support?

Dependency "Moya-SwiftyJSONMapper" has no shared framework schemes for any of the platforms: iOS

Nested JSON

I receive JSON with nested array like:

{
  "size": 20,
  "items": [
    {
      "id": "28266",
      "type": "Feed\\ItemCommented",
      "view_name": "notification_commented",
      "text": "Lorem ipsum",
      "read": false,
      "created_at": "2017-02-15T09:54:20+00:00",
    },
    {...}
  ]
}

I would like to map field "items" to array of Notification objects:
var notifications = [Notification]()
I try with such code:
self.notifications = try response.map(to: [Notification.self])
but it doesn't work because of nested items field.

Pod install fail, RxSwift version conflicts

CocoaPods version 1.0.0

[!] Unable to satisfy the following requirements:

- `RxSwift` required by `Podfile`
- `RxSwift (~> 2.4)` required by `RxPermission (0.1.1)`
- `RxSwift (~> 2.0.0)` required by `Moya-SwiftyJSONMapper/RxSwift (1.0.3)`
- `RxSwift (~> 2.0)` required by `RxNimble (0.2.0)`

Map json from key

Hi
I encountered this problem
In the app json which comes from the server looks like
{ result: [{...}, {...}] }
To parse it need create an object with variable "result" which contains an array of other objects
how can i parse array from variable "result" without create object which contains it?

Warning: 'flatMap' is deprecated

Response+SwiftyJSONMapper.swift line:32

'flatMap' is deprecated: Please use compactMap(_:) for the case where closure returns an optional value

Please update PodSpec

Please update your podspec file so that SwiftyJSON can be version 2.4.x or higher.

Why specify Moya version?

I installed this pod with 'Moya-SwiftyJSONMapper/RxSwift' and it automaticly downgrade following pods

  • Installing Moya 6.1.3 (was 6.2.0)
  • Installing NSObject+Rx 1.2.0 (was 1.2.1)
  • Installing RxCocoa 2.1.0 (was 2.2.0)
  • Installing RxSwift 2.0.0 (was 2.2.0)

Moya 10.0 compatibility

Now that there is a new version of Moya, please update the pod spec (and classes) to use the Moya v10

Unable to find a specification for `Moya-SwiftyJSONMapper (~> 6.0)`

Hi, I need this new version of Moya-swiftyJSONMapper to fit with last Alamofire (linked dependancy)
But I can't manage to have this latest version to install. At best I have the 5.0 that install.

here is my pod file on a sample project

target 'TestMoya' do
  use_frameworks!

  pod "Moya-SwiftyJSONMapper", '~> 6.0'

  target 'TestMoyaTests' do
    inherit! :search_paths
  end

  target 'TestMoyaUITests' do
  end

end

here is the response:

TestMoya % pod install
Analyzing dependencies
[!] CocoaPods could not find compatible versions for pod "Moya-SwiftyJSONMapper":
  In Podfile:
    Moya-SwiftyJSONMapper (~> 6.0)

None of your spec sources contain a spec satisfying the dependency: `Moya-SwiftyJSONMapper (~> 6.0)`.

You have either:
 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.

So I tried to force the repo location with "source" since there is a pod spec in this git. but still no luck.
Thanks for your help

nested JSON

Hi

how to do something similar to this:
name = jsonData["User_Info"]["UName"].stringValue

thanks

Throwing errors when mapping array

I have an issue when mapping over an array using the function

public func map<T: ALSwiftyJSONAble>(to type:[T.Type]) throws -> [T] {
        let jsonObject = try mapJSON()
        
        let mappedArray = JSON(jsonObject)
        let mappedObjectsArray = mappedArray.arrayValue.flatMap { T(jsonData: $0) }
        
        return mappedObjectsArray
    }

The bit that maps each item of the array is not allowed to fail because of the use of flatMap. For example, if the array contains three items of data of which two are valid, no errors will be thrown and an array of size two will be returned.

In some cases, it would be more useful if an error were thrown if the array contains invalid JSON (ie. returned nil from the init() function).

Similar to the function

public func map<T: ALSwiftyJSONAble>(to type:T.Type) throws -> T {
        let jsonObject = try mapJSON()
        
        guard let mappedObject = T(jsonData: JSON(jsonObject)) else {
            throw MoyaError.jsonMapping(self)
        }
        
        return mappedObject
    }

where the guard clause throws a MoyaError if the mapping is nil.

I can perform the map myself but I would rather contain the abstraction at its current level.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.