swift语言Codable 用法及原理详解
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>Codable </li><ul class="second_class_ul"><li>Codable 的用法</li></ul><li>JSON 和 模型的相互转换</li><ul class="second_class_ul"><li>解码(JSON Data -> Model):</li><li>编码(Model -> JSON Data):</li></ul><li>Codable 支持的数据类型</li><ul class="second_class_ul"><li>基础数据类型</li><li>Date</li><li>嵌套对象</li><li>枚举</li></ul><li>自定义 CodingKeys</li><ul class="second_class_ul"></ul><li>Codable 的原理</li><ul class="second_class_ul"><li>Decodable 协议</li><li>Container</li></ul><li>核心原理分析(Container <--> JSON)</li><ul class="second_class_ul"><li>JSONDecoder 的解码过程</li></ul><li>编译器帮我们做了什么?</li><ul class="second_class_ul"></ul><li>默认值问题</li><ul class="second_class_ul"></ul><li>属性包装器 @propertyWrapper</li><ul class="second_class_ul"><li>枚举</li><li>数组</li><li>对象</li><li>属性包装器的使用</li></ul></ul></div><p class="maodian"></p><h2>Codable </h2><p>Codable 本身就是个类型别名</p>
<div class="jb51code"><pre class="brush:cpp;">typealias Codable = Decodable & Encodable
</pre></div>
<p>代表一个同时符合 Decodable 和 Encodable 协议的类型,即可解码且可编码的类型。</p>
<p>Codable 也可以代表苹果为 Swift 开发的一套编解码系统,从 Swift 4 开始引入,包含了 Encoder 和 Decoder 协议和他们的两个实现 <code>JSONEncoder</code>、<code>JSONDecoder</code> 和 <code>PropertyListEncoder</code>、<code>PropertyListDecoder</code>。其中 Codable 及其相关协议放在了标准库中,而具体的 Encoder、Decoder 类放在了 <code>Foundation</code> 框架中。</p>
<p class="maodian"></p><h3>Codable 的用法</h3>
<p>Codable 是用来做系统自身数据结构和外部公共数据结构做转换的。系统内部数据结构可以是基础类型、结构体、枚举、类等,外部公共数据结构可以是 JSON、XML 等。</p>
<p class="maodian"></p><h2>JSON 和 模型的相互转换</h2>
<p>用 Objective-C 做 JSON 和模型转换时,一般要使用一些第三方库,这些第三方库基本上都是利用了 Objective-C Runtime 的强大特性来实现 JSON 和模型互转的。</p>
<p>但是 Swift 是一门静态语言,本身是没有像 Objective-C 那样的动态 Runtime 的。虽然在 Swift 中也可以通过继承 NSObject 的方式,来使用基于 OC Runtime 的 JSON 模型互转方案。但是这样就很不 Swift,也放弃了 Swift 作为一门静态语言的高性能,等于说自己降低了整个项目的运行性能,这是无法忍受的。</p>
<p>好在苹果提供了 <code>JSONEncoder</code> 和 <code>JSONDecoder</code> 这两个结构体来方便得在 JSON 数据和自定义模型之间互相转换。苹果可以利用一些系统私有的机制来实现转换,而不需要通过 <code>OC Runtime</code>。</p>
<p>只要让自己的数据类型符合 Codable 协议,就可以用系统提供的编解码器进行编解码。</p>
<div class="jb51code"><pre class="brush:cpp;">struct User: Codable {
var name: String
var age: Int
}
</pre></div>
<p>具体编解码代码如下:</p>
<p class="maodian"></p><h3>解码(JSON Data -> Model):</h3>
<div class="jb51code"><pre class="brush:cpp;">let json = """
{
"name": "zhangsan",
"age": 25
}
""".data(using: .utf8)!
let user = JSONDecoder().decode(User.self, from: json)
</pre></div>
<p class="maodian"></p><h3>编码(Model -> JSON Data):</h3>
<div class="jb51code"><pre class="brush:cpp;">let data = JSONEncoder().encode(user)
</pre></div>
<p class="maodian"></p><h2>Codable 支持的数据类型</h2>
<p class="maodian"></p><h3>基础数据类型</h3>
<p>在 Swift 标准库的声明文件中可以看到,基础类型都通过 <code>extension</code> 实现了 <code>Codable</code> 协议。</p>
<p>对于基础类型的属性,JSONEncoder 和 JSONDecoder 都可以正确的处理。</p>
<p class="maodian"></p><h3>Date</h3>
<p><code>JSONEncoder</code> 提供了 <code>dateEncodingStrategy</code> 属性来指定日期编码策略。 同样 <code>JSONDecoder</code> 提供了 <code>dateDecodingStrategy</code> 属性。</p>
<p>就拿 <code>dateDecodingStrategy</code> 为例,它是一个枚举类型。枚举类型有以下几个 case:</p>
<table><tbody><tr><th>case 名</th><th>作用</th></tr><tr><td>case deferredToDate</td><td>默认的 case</td></tr><tr><td>case iso8601</td><td>按照日期的 ios8601 标准来解码日期</td></tr><tr><td>case formatted(DateFormatter)</td><td>自定义日期解码策略,需要提供一个 DateFormatter 对象</td></tr><tr><td>case custom((_ decoder: Decoder) throws -> Date)</td><td>自定义日期解码策略,需要提供一个 Decoder -> Date 的闭包</td></tr></tbody></table>
<p>通常使用比较多的就是 <code>.iso8601</code> 了,因为后端返回日期通常都是已 ios8601 格式返回的。只要 JSON 中的日期是 ios8601 规范的字符串,只要设置一行代码就能让 JSONDecoder 完成日期的解码。</p>
<div class="jb51code"><pre class="brush:cpp;">struct User: Codable {
var name: String
var age: Int
var birthday: Date
}
let json = """
{
"name": "zhangsan",
"age": 25,
"birthday": "2022-09-12T10:25:41+00:00"
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let user = decoder.decode(User.self, from: json)
// user.birthday 正确解码为 Date 类型
</pre></div>
<p class="maodian"></p><h3>嵌套对象</h3>
<p>在自定义模型中嵌套对象的时候,只要这个嵌套对象也符合 Codable 协议,那整个对象就可以正常使用 <code>JSONEncoder</code> 和 <code>JSONDecoder</code> 编解码。</p>
<div class="jb51code"><pre class="brush:cpp;">struct UserInfo: Codable {
var name: String
var age: Int
}
struct User: Codable {
var info: UserInfo
}
</pre></div>
<p class="maodian"></p><p class="maodian"></p><h3>枚举</h3>
<p>枚举类型必须它的 RawValue 的类型是可解码的,并且 RawValue 的类型和 JSON 字段类型对应,即可正确解码。</p>
<p class="maodian"></p><h2>自定义 CodingKeys</h2>
<p>自定义 CodingKeys 主要是两个目的</p>
<ul><li>当数据类型属性名和 JSON 中字段名不同时,做 key 的映射。</li><li>通过在不添加某些字段的 case,来跳过某些字段的编解码过程。</li></ul>
<div class="jb51code"><pre class="brush:cpp;">struct User: Codable {
var name: String
var age: Int
var birthday: Date?
enum CodingKeys: String, CodingKey {
case name = "userName"
case age = "userAge"
}
}
</pre></div>
<p>CodingKeys 必须是一个 RawValue 为 String 类型的枚举,并符合 <code>CodingKey</code> 协议。以上代码实现的效果为,为 name 和 age 字段做了 key 映射,让编解码过程中不包含 birthday 字段。</p>
<p class="maodian"></p><h2>Codable 的原理</h2>
<p>了解了 Codable 的用法,下面我们来看一看 Codable 的原理。</p>
<p class="maodian"></p><h3>Decodable 协议</h3>
<p>由于编码和解码的原理差不多只是方向不同,我们仅探索用的更多的解码过程。</p>
<p>如果想让一个对象支持解码应该怎么做呢,当然是符合 Decodable 协议。我们先看看一个对象符合 Decodable 协议需要做哪些事情。</p>
<p><code>Decodable</code> 协议的定义如下:</p>
<div class="jb51code"><pre class="brush:cpp;">public protocol Decodable {
init(from decoder: Decoder) throws
}
</pre></div>
<p>也就是说只要实现一个传入 Decoder 参数的初始化方法,于是我们自己来实现 User。</p>
<div class="jb51code"><pre class="brush:cpp;">struct User: Decodable {
var name: String
var age: Int
init(from decoder: Decoder) throws {
}
}
</pre></div>
<p>现在要来看看怎样让 User 的两个属性的值能从 Decoder 这个对象得到。</p>
<p>查看 <code>Decoder</code> 的定义,它是一个协议。 有两个属性:</p>
<div class="jb51code"><pre class="brush:cpp;">var codingPath: { get }
var userInfo: { get }
</pre></div>
<p>还有三个方法:</p>
<div class="jb51code"><pre class="brush:cpp;">func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey
func unkeyedContainer() throws -> UnkeyedDecodingContainer
func singleValueContainer() throws -> SingleValueDecodingContainer
</pre></div>
<p>会发现这三个方法返回的都是 XXXContainer,从字面上理解是个容器,容器里面一定是容纳了某些东西。</p>
<p class="maodian"></p><h3>Container</h3>
<p>再查看这些 Container 的定义,会发现里面都有一系列 decode... 方法,来对各种类型进行 decode。</p>
<p>一共有三种类型的 Container:</p>
<table><tbody><tr><th>Container 类型</th><th>作用</th></tr><tr><td>SingleValueDecodingContainer</td><td>代表容器中只保存了一个值</td></tr><tr><td>KeyedDecodingContainer</td><td>代表容器中保存的数据是按照键值对的形式保存的</td></tr><tr><td>UnkeyedDecodingContainer</td><td>代表容器中保存的数据是没有键的,也就是说,保存的数据是一个数组</td></tr></tbody></table>
<p>回到上面 User 的例子,JSON 数据如下:</p>
<div class="jb51code"><pre class="brush:cpp;">{
"user": "zhangsan",
"age": 25
}
</pre></div>
<p>这种数据显然是键值对,因此要用 KeyedDecodingContainer 来取数据。KeyedDecodingContainer 应该是最常用的 Container 了。</p>
<div class="jb51code"><pre class="brush:cpp;">struct User: Decodable {
var name: String
var age: Int
init(from decoder: Decoder) throws {
decoder.container(keyedBy: &lt;#T##CodingKey.Protocol#&gt;)
}
}
</pre></div>
<p>参数需要传一个符合 CodingKey 协议的对象的类型,于是这里必须自己实现 CodingKeys 枚举,并把 CodingKeys.self 传入参数。</p>
<div class="jb51code"><pre class="brush:cpp;">struct User: Decodable {
var name: String
var age: Int
enum CodingKeys: String, CodingKey {
case name
case age
}
init(from decoder: Decoder) throws {
let container = decoder.container(keyedBy: CodingKeys.self)
}
}
</pre></div>
<p>然后就可以从 container 中取数据出来赋给自身的属性。由于这几个方法都会抛出异常,因此都要加上 <code>try</code>。</p>
<div class="jb51code"><pre class="brush:cpp;">init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
age = try container.decode(Int.self, forKey: .age)
}
</pre></div>
<p>同样的,我们也可以实现出编码。这时把 User 实现的协议改成 <code>Codable</code>。</p>
<div class="jb51code"><pre class="brush:cpp;">struct User: Codable {
var name: String
var age: Int
enum CodingKeys: String, CodingKey {
case name
case age
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
age = try container.decode(Int.self, forKey: .age)
}
func encode(to encoder: Encoder) throws {
var encoder = encoder.container(keyedBy: CodingKeys.self)
try encoder.encode(name, forKey: .name)
try encoder.encode(age, forKey: .age)
}
}
</pre></div>
<p>编码的过程就是和解码反过来,因为是键值对,从 encoder 中拿到 <code>KeyedEncoderContainer</code>,然后调用 encode 方法把属性的数据编码到 container 中,然后由 <code>JSONEncoder</code> 来处理接下来的事情。</p>
<p>接下来我们好奇的是,Container 中的数据是怎么保存的,Container 中的数据和 JSON 又是怎么互相转换的。</p>
<p class="maodian"></p><h2>核心原理分析(Container <--> JSON)</h2>
<p class="maodian"></p><h3>JSONDecoder 的解码过程</h3>
<p>从 <code>JSONDecoder().decode(User.self, from: json)</code> 这句开始分析。打开 swift-corelibs-foundation 中 <code>JSONDecoder</code> 的源码。</p>
<div class="jb51code"><pre class="brush:cpp;">// 1
var parser = JSONParser(bytes: Array(data))
let json = try parser.parse()
// 2
return try JSONDecoderImpl(userInfo: self.userInfo, from: json, codingPath: [], options: self.options)
.unwrap(as: T.self) // 3
</pre></div>
<p>decode 方法的实现主要是这三行代码。</p>
<ul><li>先把 data 转化为一个类型为 <code>JSONValue</code> 的 json 对象。</li><li>然后构造一个 JSONDecoderImpl 对象</li><li>调用 JSONDecoderImpl 对象的 <code>unwrap</code> 方法得到要转换成的对象。</li></ul>
<p>查看 <code>JSONValue</code> 的定义,它通过枚举嵌套把 JSON 的类型定义了出来。具体的数据通过关联值携带在了这个枚举类型中。</p>
<div class="jb51code"><pre class="brush:cpp;">enum JSONValue: Equatable {
case string(String)
case number(String)
case bool(Bool)
case null
case array()
case object()
}
</pre></div>
<p>在获取 KeyedDecodingContainer 的时候也就是通过 JSONValue 构建 Container 对象。</p>
<div class="jb51code"><pre class="brush:cpp;">// 这里 self.json 是保存在 JSONDecoderImpl 中的 JSONValue 类型
switch self.json {
case .object(let dictionary): // JSONValue 和 .object 这个 case 匹配,取出字典数据
let container = KeyedContainer<Key>(
impl: self,
codingPath: codingPath,
dictionary: dictionary // 传入字典数据
)
return KeyedDecodingContainer(container)
</pre></div>
<p>可以看到,<code>KeyedDecodingContainer</code> 只有当 <code>self.json</code> 匹配为字典时才能正确创建。数据在里面以 <code>let dictionary: </code> 形式保存。</p>
<p>再看其他代码可以发现:</p>
<p><code>SingleValueContainer</code> 就是直接存了一个 <code>let value: JSONValue</code> 在里面。</p>
<p><code>UnkeyedDecodingContainer</code> 则是存了一个数组 <code>let array: </code>。</p>
<p>因此在 Container 调用 <code>decode</code> 方法获取数据时,就是根据参数 key 和类型从自身保存的数据中获取数据。这个源码很简单,看一下就明白了。</p>
<p>最后一步的 unwrap 方法,通过源码可以看到,最终调用的就是对象自己实现的 <code>init(from decoder: Decoder)</code> 方法</p>
<p>因此可以得出 <strong>JSON -> Model 的步骤</strong>如下:</p>
<ul><li>JSONParser 对传入的二进制 JSON data 进行解析,解析为 JSONValue 对象。</li><li>构建 JSONDecoderImpl,将相关的数据保存在里面。</li><li>调用 JSONDecoderImpl 的 unwrap 方法,开始调用对象实现的 <code>init(from: decoder: Decoder)</code> 方法</li><li>在 ``init(from: decoder: Decoder)` 方法中,首先根据数据类型获取对应的 Container。</li><li>调用 Container 的 decodeXXX 方法得到具体的值赋值给属性。</li></ul>
<p>Model -> JSON 的步骤也是差不多的,只是方向反过来,有兴趣可以自己看一下源码。</p>
<p>在 Swift 的 JSON 转模型方法中,通过观察 Github 上的开源库可以发现一共有三种实现方案:</p>
<ul><li><strong>Objective-C Runtime</strong> 一众本身就是 OC 开发的库基本都用的这个方案,比如 YYModel,这种方案使用起来非常简单,代码非常少,但不符合 Swift。</li><li><strong>Key 映射</strong> 比如 ObjectMapper 就是这种,这种的缺点是每个对象都要写一大堆映射代码,比较麻烦</li><li><strong>利用对象底层内存布局</strong> SwiftyJSON 就属于这种,这种方法使用起来一样很方便,但是依赖苹果的私有代码,苹果如果调整了内部实现就会失效。</li></ul>
<p>通过上面分析 Codable 原理发现,Codable 基本上就是 Key 映射的方案,只不过编译器帮我们自动合成了很多代码来让我们使用起来一样可以非常简单。由于编译器不会帮第三方库合成代码,因此 Codable 秒杀了一众基于 key 映射实现的第三方库。</p>
<p class="maodian"></p><h2>编译器帮我们做了什么?</h2>
<p>我们发现,只要让自己的对象符合 Codable 协议,就可以正常用 <code>JSONEncoder</code> 和 <code>JSONDecoder</code> 编解码,并不需要实现协议中定义的方法。</p>
<p>那是因为编译器帮我们生成了。这种编译器合成代码在很多地方都会用到,例如为结构体和枚举自动合成实现 Equatable 和 Hashable 的代码,为枚举合成实现 CaseIterable 的代码等。</p>
<p>上面的 User 例子,编译器为我们合成的代码如下:</p>
<div class="jb51code"><pre class="brush:cpp;">struct User: Codable {
var name: String
var age: Int
// 编译器合成
enum CodingKeys: String, CodingKey {
case name
case age
}
// 编译器合成
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
age = try container.decode(Int.self, forKey: .age)
}
// 编译器合成
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(age, forKey: .age)
}
}
</pre></div>
<p>可以见到,编译器自动合成了 CodingKeys 枚举的定义,并合成了实现 Encodable 和 Decodable 协议的代码。这给开发人员提供了方便。</p>
<p class="maodian"></p><h2>默认值问题</h2>
<p>编译器自动生成的编解码实现有个问题就是不支持默认值。如果需要支持默认值就需要自己来用 <code>decodeIfPresent</code> 来实现:</p>
<div class="jb51code"><pre class="brush:cpp;">struct User: Decodable {
var name: String
var age: Int
enum CodingKeys: String, CodingKey {
case name
case age
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decodeIfPresent(String.self, forKey: .name) ?? ""
age = try container.decodeIfPresent(Int.self, forKey: .age) ?? 0
}
}
</pre></div>
<p>但是这样每个结构体都要自己实现一次,非常麻烦。其实这个网上已经有很多文章在说了,就是用 <code>@propertyWrapper</code> 属性包装器来解决这个问题。</p>
<p class="maodian"></p><h2>属性包装器 @propertyWrapper</h2>
<p>属性包装器用来给属性和定义属性的结构之间包装一层,用来实现一些通用的 setter 和 getter 逻辑或初始化逻辑等。</p>
<p>例如对于 Int 型,可以如下定义属性包装器。</p>
<div class="jb51code"><pre class="brush:cpp;">@propertyWrapper
public struct DefaultInt: Codable {
public var wrappedValue: Int
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
wrappedValue = (try? container.decode(BaseType.self)) ?? 0
}
public func encode(to encoder: Encoder) throws {
try wrappedValue.encode(to: encoder)
}
}
</pre></div>
<p>以上代码实现了 <code>init(from decoder: Decoder)</code> 方法来为属性在解码失败时提供一个默认值 0。实现 <code>encode(to encoder: Encoder)</code> 是为了编码时直接编码内部值而不是编码整个属性包装类型。</p>
<p>其它的很多基础类型都是一样的逻辑,为了避免重复代码,可以用范型来统一实现。</p>
<div class="jb51code"><pre class="brush:cpp;">public protocol HasDefaultValue {
static var defaultValue: Self { get set }
}
@propertyWrapper
public struct DefaultBaseType&lt;BaseType: Codable &amp; HasDefaultValue&gt;: Codable {
public var wrappedValue: BaseType
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
wrappedValue = (try? container.decode(BaseType.self)) ?? BaseType.defaultValue
}
public func encode(to encoder: Encoder) throws {
try wrappedValue.encode(to: encoder)
}
}
</pre></div>
<p>然后可以考虑用类型别名来定义出各个类型的属性包装关键字。因为如果包含 <code><</code> 或 <code>.</code> 等字符,写起来会比较麻烦。</p>
<div class="jb51code"><pre class="brush:cpp;">typealias DefaultInt = DefaultBaseType<Int>
typealias DefaultString = DefaultBaseType<String>
</pre></div>
<p>但是有些类型需要特殊实现一下。</p>
<h3>枚举</h3>
<p>枚举类型可以利用 rawValue 来进行数据和类型相互转换。</p>
<div class="jb51code"><pre class="brush:cpp;">@propertyWrapper
public struct DefaultIntEnum<Value: RawRepresentable & HasDefaultEnumValue>: Codable where Value.RawValue == Int {
private var intValue = Value.defaultValue.rawValue
public var wrappedValue: Value {
get { Value(rawValue: intValue)! }
set { intValue = newValue.rawValue }
}
public init() {
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
intValue = (try? container.decode(Int.self)) ?? Value.defaultValue.rawValue
}
public func encode(to encoder: Encoder) throws {
try intValue.encode(to: encoder)
}
}
</pre></div>
<p class="maodian"></p><h3>数组</h3>
<p>由于数组需要通过 UnkeyedDecodingContainer 拿数据,需要单独特殊处理。</p>
<div class="jb51code"><pre class="brush:cpp;">@propertyWrapper
public struct DefaultArray<Value: Codable>: Codable {
public var wrappedValue:
public init() {
wrappedValue = []
}
public init(wrappedValue: ) {
self.wrappedValue = wrappedValue
}
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
var results = ()
while !container.isAtEnd {
let value = try container.decode(Value.self)
results.append(value)
}
wrappedValue = results
}
public func encode(to encoder: Encoder) throws {
try wrappedValue.encode(to: encoder)
}
}
</pre></div>
<p class="maodian"></p><h3>对象</h3>
<p>因为对象的结构都是不一样的,没法给出一个的默认值。因此设计了一个 <code>EmptyInitializable</code> 协议,里面只有一个无参数的初始化方法。</p>
<div class="jb51code"><pre class="brush:cpp;">public protocol EmptyInitializable {
init()
}
</pre></div>
<p>需要提供默认值的对象可以实现这个协议。不过这里需要权衡一下,如果对内存空间占用有比较高的要求,用可选值可能是更好的方案,因为一个空对象占用的空间和有数据的对象占用的空间是一样多的。</p>
<p class="maodian"></p><h3>属性包装器的使用</h3>
<p>使用属性包装器封装各个类型后,只要像这样使用就可以了,decode 的时候就如果不存在对应字段数据属性就会初始化为默认值。</p>
<div class="jb51code"><pre class="brush:cpp;">struct User {
@DefaultString var name: String
@DefaultInt var age: Int
}
</pre></div>
<p>我简单封装了一个库,目前我们的新 Swift 项目在使用,完整代码在这里: github.com/liuduoios/C…</p>
<p>参考资料:</p>
<p>《the swift programming language》</p>
<p>《Advanced Swift》</p>
<p>swift-corelibs-foundation 源码</p>
<p>以上就是swift语言Codable 用法及原理详解的详细内容,更多关于swift Codable用法原理的资料请关注琼殿技术社区其它相关文章!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>Swift利用Decodable解析JSON的一个小问题详解</li><li>关于Swift 4.1中的Codable改进详解</li><li>Swift使用编解码库Codable的过程详解</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]