测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2018年3月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Narek Mailian 维护。
要将模型创建为可序列化和可反序列化
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 文件以获取更多信息。