Latest 1.0.3
Homepage https://github.com/ApterKing/SwiftyJSONMappable
License MIT
Platforms ios 8.0, requires ARC
Authors

Enclose SwiftyJSON which implement:

  • JSON->Model : Transform JsonString or Dictionary/Array etc to Model, for more information SwiftyJSON

  • Model->JSON : Transform Model which impements protocol JSONConvertibleMappable

中文文档

Installation

You can use cocoapod utility to import it to your Project

pod 'SwiftyJSONMappable'

Or : Use it with Moya

pod 'SwiftyJSONMappable/Moya'

Or : Use it with RxSwift

pod 'SwiftyJSONMappable/RxSwift'

Usage

Model define

class HttpBin: JSONMappable {
    var args: [String: Any]
    var headers: HttpBinHeaders
    var origin: String
    var url: String

    required init(json: JSON) {
        args = json["args"].dictionaryValue
        headers = HttpBinHeaders(json: json["headers"])
        origin = json["origin"].stringValue
        url = json["url"].stringValue
    }
}

Transform Model to JSON or JSONString

let httpBin = HttpBin(json: jsonData)

let JSON = httpBin.mapJSON()    // transform to SwiftyJSON
let JSONString = httpBin.mapString()   // transform to json String

You will get Result like this:

{
  "headers" : {
    "acceptEncoding" : "gzip;q=1.0, compress;q=0.5",
    "acceptLanguage" : "en;q=1.0",
    "connection" : "close",
    "accept" : "*/*",
    "host" : "httpbin.org"
  },
  "origin" : "118.113.69.83",
  "args" : {

  },
  "url" : "https://httpbin.org/get"
}

Ignore and Replace Property when transfrom to JSON/JSONString

class HttpBin: JSONMappable {
    var args: [String: Any]
    var headers: HttpBinHeaders
    var origin: String
    var url: String

    required init(json: JSON) {
        args = json["args"].dictionaryValue
        headers = HttpBinHeaders(json: json["headers"])
        origin = json["origin"].stringValue
        url = json["url"].stringValue
    }

    /// Set the ignore Property's Names
    public var ignoreProperties: [String]? {
    return ["args"]
    }

    /// Set the replace Properties
    /// [originName: newName]
    public var replacedProperties: [String : String]? {
        return ["origin": "newOrigin"]
    }
}

You will get Result like this:

{
  "newOrigin" : "118.113.69.83",
  "url" : "https://httpbin.org/get",
  "headers" : {
    "acceptEncoding" : "gzip;q=1.0, compress;q=0.5",
    "acceptLanguage" : "en;q=1.0",
    "connection" : "close",
    "accept" : "*/*",
    "host" : "httpbin.org"
  }
}

Use with Moya

Request data from: Here

MoyaProvider<APIService>().request(.testGet) { (result) in
    switch result {
    case let .success(response):
        do {
            let httpBin = try response.mapJSONMappable(HttpBin.self)
            print(httpBin.mapString() ?? "")
        } catch {
            print(error)
        }
    case let .failure(error):
        print(error)
    }
}

Use with RxSwift

RxMoyaProvider<APIService>().request(.testGet)
     .mapJSONMappable(HttpBin.self)
     .observeOn(SerialDispatchQueueScheduler(internalSerialQueueName: "test"))
     .subscribe { (event) in
          switch event {
          case let .next(httpBin):
               print(httpBin.mapString() ?? "请求完毕")
          case let .error(error):
               print(error)
          default:
               print(event)
          }
     }.addDisposableTo(disposeBag)

Author

ApterKing, [email protected]

License

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

Latest podspec

{
    "name": "SwiftyJSONMappable",
    "version": "1.0.3",
    "summary": "SwiftyJSON extensions JSON->Model  Model->JSON  model->JSONString.",
    "description": "JSONMappable implement [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON)nResponse+JSONMappable [Moya](https://github.com/Moya/Moya)nObservable+JSONMappable [RxSwift](https://github.com/ReactiveX/RxSwift)",
    "homepage": "https://github.com/ApterKing/SwiftyJSONMappable",
    "license": "MIT",
    "authors": {
        "ApterKing": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/ApterKing/SwiftyJSONMappable.git",
        "tag": "1.0.3"
    },
    "requires_arc": true,
    "default_subspecs": "Mappable",
    "pushed_with_swift_version": "3.0",
    "subspecs": [
        {
            "name": "Mappable",
            "source_files": "Pod/Classes/JSONMappable.swift",
            "dependencies": {
                "SwiftyJSON": [
                    "~>3.1.4"
                ]
            },
            "frameworks": "Foundation"
        },
        {
            "name": "Moya",
            "source_files": "Pod/Classes/Response+JSONMappable.swift",
            "dependencies": {
                "Moya": [],
                "SwiftyJSONMappable/Mappable": []
            }
        },
        {
            "name": "RxSwift",
            "source_files": "Pod/Classes/Observable+JSONMappable.swift",
            "dependencies": {
                "Moya/RxSwift": [],
                "SwiftyJSONMappable/Moya": []
            }
        }
    ]
}

Pin It on Pinterest

Share This