从Swift4开始提供的Decodable解析JSON确实很方便,但遇到一个小问题,记录一下。

当JSON中某个key的值为{}或者空字符串”“,而该值需要解析的不是基本类型时,即使标记为 Optional,依然会导致整个解析失败:

//: Playground

import Foundation

//Book.swift
struct Book: Codable {

	var id: Int
	var name: String
	var cover: BookCover?

	enum CodingKeys: String, CodingKey {
		case id
		case name
		case cover
	}

	struct BookCover: Codable {

		var url: String
		var thumbURL: String

		enum CodingKeys: String, CodingKey {
			case url
			case thumbURL = "thumb_url"
		}
	}
}

//JSON
let bookJSON1 =
"""
{
	"id": 1,
	"name": "fake name 1",
	"cover": {
		"url": "a.png",
		"thumb_url": "b.png"
	}
}
"""

let bookJSON2 =
"""
{
	"id": 2,
	"name": "fake name 2",
	"cover": {

	}
}
"""

//解析
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let book1 = try? decoder.decode(Book.self, from: bookJSON1.data(using: .utf8)!)

// 解析正常
print(book1)

let book2 = try? decoder.decode(Book.self, from: bookJSON2.data(using: .utf8)!)
// 输出 nil,cover已经是 Optional,为何整个book都解析失败?
print(book2)

原因:

因为cover是 Optional,所以会调用 decodeIfPresent 来解析,而在cover节点中没有找到url,thumb_url这两个key,导致默认解析失败,直接抛出了错误。

解决:

重新实现 decodeIfPresent,在解析失败时返回nil而不是抛出错误导致整个解析失败:


extension KeyedDecodingContainer {
	public func decodeIfPresent<T>(_ type: T.Type, forKey key: K) throws -> T? where T : Decodable {
		return try? decode(type, forKey: key)
	}
}

参考: http://davelyon.net/2017/08/16/jsondecoder-in-the-real-world