Juice 1.0.10

Juice 1.0.10

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2018年3月
SwiftSwift 版本3.0
SPM支持 SPM

Narek Mailian 维护。



Juice 1.0.10

  • 作者:
  • Narek Mailian

Juice

🍎Juice 是一个适用于 Swift 3 的新型简单的 JSON 编解码器🍎

功能

  • 编写以充分利用 Swift 抛出错误的能力。
  • 如果缺少必要的参数或类型不正确,Juice 将会指明确切的模型和键 - 方便调试。
  • 易于使用。没有奇特的 `<*>`, `<|?>`, `<:3>` 运算符。只需使用 `decode`,Juice 会自动完成其它操作。
  • 具有编码/解码时转换值的功能。
  • 当然,还有许多测试用例!

用法

要将模型创建为可序列化和可反序列化

struct Person: Decodable, Encodable {
    let title: String
    let firstName: String
    let lastName: String
    let age: Int
    let car: Car?
    let children: [Person]
    
    init(fromJson json: JSONDictionary) throws {
        title = try json.decode(["name", "title"])
        firstName = try json.decode(["name", "fullName", "firstName"])
        lastName = try json.decode(["name", "fullName", "lastName"])
        age = try json.decode("age")
        car = try json.decode("car")
        children = try json.decode("children")
    }
    
    func encode() throws -> JSONDictionary {
        var dictionary = JSONDictionary()
        try dictionary.encode(["name", "title"], value: title)
        try dictionary.encode(["name", "fullName", "firstName"], value: firstName)
        try dictionary.encode(["name", "fullName", "lastName"], value: lastName)
        try dictionary.encode(["age"], value: age)
        try dictionary.encode(["car"], value: car)
        try dictionary.encode(["children"], value: children)
        return dictionary
    }
}

具有原始值的可序列化和可反序列化枚举

enum PhoneNumberType: String, Decodable, Encodable {
    case home
    case office
    case mobile
}

无原始值的可序列化枚举

enum Distance: Decodable {
    case reallyClose
    case close
    case far
    
    init(fromJson json: Double) throws {
        switch json {
        case _ where json < 2:
            self = .reallyClose
        case _ where json < 6:
            self = .close
        default:
            self = .far
        }
    }
}

使用 Juice 可以通过以下方式解码任何类型...

struct TransformHomePage {
    let title: String
    let url: URL
    
    init(fromJson json: JSONDictionary) throws {
        title = try json.decode("title")
        url = try json.decode("url", transform: { (input: String) -> URL in
            if let url = URL(string: input) {
                return url
            } else {
                throw BadURLError(attemptedUrl: input)
            }
        })
    }
}

...或通过使用协议扩展

extension URL: Decodable {
    public init(fromJson json: String) throws {
        if let url = URL(string: json) {
            self = url
        } else {
            throw BadURLError(attemptedUrl: json)
        }
    }
}

struct ProtocolHomePage {
    let title: String
    let url: URL
    
    init(fromJson json: JSONDictionary) throws {
        title = try json.decode("title")
        url = try json.decode("url")
    }
}

...并在无法直接修改的类上...

extension NSURL: FactoryDecodable {
    public static func create<T>(fromJson json: String) throws -> T {
        if let url = NSURL(string: json) {
            return url as! T
        } else {
            throw BadURLError(attemptedUrl: json)
        }
    }
}

struct ProtocolHomePage {
    let title: String
    let url: NSURL
    
    init(fromJson json: JSONDictionary) throws {
        title = try json.decode("title")
        url = try json.decode("url")
    }
}

Juice 特有的精确错误抛出

do {
    let dict = JSONDictionary(["title": "Apple", "url": "ht tps://apple.com"])
    try ProtocolHomePage(fromJson: dict)
} catch {
    // Error at key path ["url"]: Failure when attempting to decode type URL: Not a valid URL: "ht tps://apple.com"
    print(error.localizedDescription)
}

示例

要运行示例游乐场,克隆 repo,然后首先从 Example 目录运行 `pod install`。然后,打开 `Juice.xcworkspace` 并在游乐场中尝试!也可以查看单元测试以获取更多信息。

要求

Juice 已测试适用于 iOS 8.0+

安装

Juice 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile:

pod "Juice"

作者

Narek Mailian, [email protected]

许可证

Juice 提供 MIT 许可证。请查看 LICENSE 文件以获取更多信息。