测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2017年5月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Alex Hmelevski 维护。
AHJSONSerializer 是一个 Swift 编写的框架,它使您能够轻松地将模型对象(类和结构体)以及其 JSON 格式之间进行转换。
为了支持 json 解码,只需实现 JSONDecodable
协议的类或结构体
public protocol JSONDecodable {
init(decoder: AHJSONDecoder)
}
为了支持 json 编码,类或结构体需要实现 JSONEncodable
协议
public protocol JSONEncodable {
func encode(with encoder: AHJSONEncoder)
}
AHJSONSerializer 使用 `<~` 和 `~>` 语法糖操作符来定义每个成员变量如何映射到和从 JSON。
class User: JSONDecodable,JSONEncodable {
var username: String?
var age: Int?
var weight: Double!
var array: [AnyObject]?
var dictionary: [String : AnyObject] = [:]
var bestFriend: User? // Nested User object
var friends: [User]? // Array of Users
var birthday: Date?
required init(decoder: AHJSONDecoder) {
username <~ decoder["username"]
age <~ decoder["age"]
weight <~ decoder["weight"]
array <~ decoder["arr"]
dictionary <~ decoder["dict"]
bestFriend <~ decoder["best_friend"]
friends <~ decoder["friends"]
birthday <~ decoder["birthday"]
}
func encode(with encoder: AHJSONEncoder) {
username ~> encoder["username"]
age ~> encoder["age"]
weight ~> encoder["weight"]
array ~> encoder["arr"]
dictionary ~> encoder["dict"]
bestFriend ~> encoder["best_friend"]
friends ~> encoder["friends"]
birthday ~> encoder["birthday"]
}
}
struct Temperature: JSONDecodable {
let celsius: Double
let fahrenheit: Double?
init(decoder: AHJSONDecoder) {
celsius = decoder["temp_in_c"].value() ?? 0.0
fahrenheit = decoder["temp_in_f"].value()
}
}
一旦类实现了 JSONDecodable
和 JSONEncodable
,AHJSONSerializer 就允许您轻松地将对象转换成和转换成 JSON。
将 JSON 字符串转换为模型对象
let user = Object(json: dictionary)
将模型对象转换为 JSON 字典
let json = user.json
AHJSONSerializer 可以将以下类型的类映射
Int
Bool
Double
Float
String
RawRepresentable
(枚举)Array<AnyObject>
Dictionary<String, AnyObject>
Object<T: JSONDecodable>
Array<T: JSONDecodable>
Array<Array<T: JSONDecodable>>
Set<T: JSONDecodable>
Dictionary<String, T: JSONDecodable>
Dictionary<String, Array<T: JSONDecodable>>
JSONDecodable
协议init(decoder: AHJSONDecoder)
这个函数是从所有映射定义的地方。当解析 JSON 时,在对象创建期间执行此函数。当生成 JSON 时,这是对对象调用的唯一函数。
JSONEncodable
协议init(decoder: AHJSONEncoder)
所有编码定义都应该放在这个函数中。该函数会在调用 Object.json
时使用
AHJSONSerializer 支持在键中使用点符号,以方便嵌套对象的映射。给定以下 JSON 字符串
"distance" : {
"text" : "102 ft",
"value" : 31
}
你可以按如下方式访问嵌套对象
func init(decoder: AHJSONDecoder) {
distance <~ map["distance.value"]
}
AHJSONSerializer 支持使用 map
函数,可以进行一些疯狂的操作
struct Car: JSONDecodable, JSONEncodable {
let model: String
init(decoder: AHJSONDecoder) {
model = decoder["model"].map(transform: transformToInt)
.map(transform: increase)
.map(transform: transformToString)
.value() ?? ""
}
func encode(with encoder: AHJSONEncoder) {
model ~> encoder["firstName"].map{uppercased}
}
private func uppercased(str: String) -> String {
return str.uppercased()
}
}
建议 map
函数是强类型的(没有泛型)
实现 JSONDecodable
和 JSONEncodable
协议的类很容易被子类化。当子类化映射类时,请按照以下结构进行
class Base: JSONDecodable {
var base: String?
required init(decoder: AHJSONDecoder) {
base <~ map["base"]
}
}
class Subclass: Base {
var sub: String?
override init(decoder: AHJSONDecoder) {
super.init(decoder)
sub <~ map["sub"]
}
}
确保你的子类实现调用了正确的初始化器和映射函数,以应用父类的映射。
AHJSONSerializer 可以处理泛型类型为类的类,只要泛型类型也符合 JSONDecodable
。见以下示例
class Result<T: JSONDecodable>: JSONDecodable {
var result: T?
required init(decoder: AHJSONDecoder) {
result <~ map["result"]
}
}
非常欢迎投稿
在提交任何拉取请求之前,请确保您已运行包含的测试,并且它们已经通过。如果您正在包含新功能,请为此编写测试用例。
要将 AHJSONSerializer 添加到基于 Swift 包管理器 的项目中,请在您的 Package.swift
文件的 dependencies
数组中添加
.Package(url: "hhttps://github.com/AlexHmelevski/AHJSONSerializer.git", majorVersion: 2, minor: 2),
。
Alexei Hmelevski,[email protected]
AHJSONSerializer 基于 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。