Latest 0.8.2
Homepage https://github.com/Pircate/CleanJSON
License MIT
Platforms ios 9.0, osx 10.10, tvos 9.0, watchos 2.0
Authors

CI Status
Version
Carthage compatible
License
Platform
codebeat badge

继承自 JSONDecoder,在标准库源码基础上做了改动,以解决 JSONDecoder 各种解析失败的问题,如键值不存在,值为 null,类型不一致。

只需将 JSONDecoder 替换成 CleanJSONDecoder,属性可以全部使用不可选类型。

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 9.0
  • Swift 5.0

Installation

CleanJSON is available through CocoaPods or Carthage. To install
it, simply add the following line to your Podfile or Cartfile:

Podfile

pod 'CleanJSON'

Cartfile

github "Pircate/CleanJSON"

Import

import CleanJSON

Usage

Normal

let decoder = CleanJSONDecoder()
try decoder.decode(Model.self, from: data)

Enum

对于不可选的枚举类型请遵循 CaseDefaultable 协议,如果解析失败会返回默认 case

Note: 枚举使用强类型解析,关联类型和数据类型不一致不会进行类型转换,会解析为默认 case

enum Enum: Int, Codable, CaseDefaultable {

    case case1
    case case2
    case case3

    static var defaultCase: Enum {
        return .case1
    }
}

Customize decoding strategy

可以通过 valueNotFoundDecodingStrategy 在值为 null 或类型不匹配的时候自定义解码,默认策略请看这里

struct CustomAdapter: JSONAdapter {

    // 由于 Swift 布尔类型不是非 0 即 true,所以默认没有提供类型转换。
    // 如果想实现 Int 转 Bool 可以自定义解码。
    func adapt(_ decoder: CleanDecoder) throws -> Bool {
        // 值为 null
        if decoder.decodeNil() {
            return false
        }

        if let intValue = try decoder.decodeIfPresent(Int.self) {
            // 类型不匹配,期望 Bool 类型,实际是 Int 类型
            return intValue != 0
        }

        return false
    }
}

decoder.valueNotFoundDecodingStrategy = .custom(CustomAdapter())

For Moya

使用 Moya.Response 自带的 map 方法解析,传入 CleanJSONDecoder

provider = MoyaProvider<GitHub>()
provider.request(.zen) { result in
    switch result {
    case let .success(response):
        let decoder = CleanJSONDecoder()
        let model = response.map(Model.self, using: decoder)
    case let .failure(error):
        // this means there was a network failure - either the request
        // wasn't sent (connectivity), or no response was received (server
        // timed out).  If the server responds with a 4xx or 5xx error, that
        // will be sent as a ".success"-ful response.
    }
}

For RxMoya

provider = MoyaProvider<GitHub>()

let decoder = CleanJSONDecoder()
provider.rx.request(.userProfile("ashfurrow"))
    .map(Model.self, using: decoder)
    .subscribe { event in
        switch event {
        case let .success(model):
            // do someting
        case let .error(error):
            print(error)
        }
    }

Author

Pircate, [email protected]

License

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

Latest podspec

{
    "name": "CleanJSON",
    "version": "0.8.2",
    "summary": "Custom JSON decoder for Decodable.",
    "homepage": "https://github.com/Pircate/CleanJSON",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Pircate": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Pircate/CleanJSON.git",
        "tag": "0.8.2"
    },
    "platforms": {
        "ios": "9.0",
        "osx": "10.10",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "swift_versions": "5.0",
    "source_files": "CleanJSON/Classes/**/*"
}

Pin It on Pinterest

Share This