如何以自定义方式对已经符合 Codable 的基础类型(例如 TimeInterval)进行编码?
How to encode Foundation types that already conform to Codable (e.g. TimeInterval) in a custom way?
在 Swift 4 中以自定义方式将自定义类型编码为 JSON 很容易,并且有很好的文档可用。但是我在以自定义方式编码 Foundation
类型时遇到了问题。我的属性类型为 TimeInterval
的自定义类型需要 Codable
,格式为 JSON,显示分钟和秒,如下所示:
// my type:
struct MyType: Codable {
let time: TimeInterval = 65.12
}
// required JSON representation:
"myType": {
"time":
{
"minutes": 1,
"seconds": 5.12
}
}
如果我为我的每个类型提供 encode(to:)
和 init(decoder:)
的自定义实现,这很好用。但我不想重复我的话,只是为 TimeInterval
提供这些方法的自定义版本。所以我尝试了这样的扩展:
extension TimeInterval {
enum CodingKeys: String, CodingKey {
case minutes
case seconds
}
func encode(to encoder: Encoder) throws {
let minutes = (NSInteger(self) / 60) % 60
let seconds = self.truncatingRemainder(dividingBy: 60)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(minutes, forKey: .minutes)
try container.encode(seconds, forKey: .seconds)
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
guard let minutes = try? values.decode(Int.self, forKey: .minutes), let seconds = try? values.decode(Double.self, forKey: .seconds) else {
throw NSError()
}
self = TimeInterval(Double(minutes) * 60.0 + seconds)
}
}
不幸的是,此代码永远不会被调用:编码 TimeInterval
值似乎总是使用编码 Double
的默认实现,它符合开箱即用的 Codable
。
let myType = MyType(time: 65.12)
let encoded = try! JSONEncoder().encode(myType)
let json = String(data: encoded, encoding: .utf8)!
print(json)
// prints:
// "myType":
// {
// "time": 65.12
// }
我该如何解决这个问题?
编辑:
就像我在第一句话中写的那样,我不是在寻找解决方案,如何编码我自己的自定义类型。这在我的代码中已经运行良好。我正在尝试 更改 基础类型 TimeInterval
的编码行为。
我修改了您的代码(仅作为示例),并且有效。
struct MyType {
let time: TimeInterval = 65.12
enum CodingKeys: String, CodingKey {
case minutes
case seconds
}
}
extension MyType: Encodable {
func encode(to encoder: Encoder) throws {
let minutes = 20
let seconds = 30
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(minutes, forKey: .minutes)
try container.encode(seconds, forKey: .seconds)
}
}
let myType = MyType()
let encoded = try! JSONEncoder().encode(myType)
let json = String(data: encoded, encoding: .utf8)!
print(json)
enum MyCodingKeys: String, CodingKey {
case minutes
case seconds
}
protocol TimeIntervalEncodable: Encodable {
var time: TimeInterval {get set}
}
extension TimeIntervalEncodable {
func encode(to encoder: Encoder) throws {
let minutes = (Int(time) / 60) % 60
let seconds = time.truncatingRemainder(dividingBy: 60)
var container = encoder.container(keyedBy: MyCodingKeys.self)
try container.encode(minutes, forKey: .minutes)
try container.encode(seconds, forKey: .seconds)
}
}
struct MyType: TimeIntervalEncodable {
var time: TimeInterval = 65.12
}
let myType = MyType()
let encoded = try! JSONEncoder().encode(myType)
let json = String(data: encoded, encoding: .utf8)!
print(json)
您可以将您的时间间隔设为只读计算 属性 并添加一个采用 TimeInterval 的自定义初始化程序并初始化您的时间子结构属性:
struct Root: Codable {
let myType: MyType
}
struct MyType: Codable {
let time: Time
struct Time: Codable {
let minutes: Int
let seconds: Double
}
init(time: TimeInterval) {
self.time = Time(minutes: (Int(time) / 60) % 60, seconds: time.truncatingRemainder(dividingBy: 60))
}
var timeInterval: TimeInterval {
return TimeInterval(time.minutes * 60) + time.seconds
}
}
let root = Root(myType: MyType(time: 65.12))
do {
let encoded = try JSONEncoder().encode(root)
print(String(data: encoded, encoding: .utf8)!) // "{"myType":{"time":{"minutes":1,"seconds":5.1200000000000045}}}\n
} catch {
print(error)
}
edit/update:
如果您需要将时间 属性 添加到多个结构中,您可以将结构时间从自定义类型中移出并创建一个定时协议:
struct Time: Codable {
let minutes: Int
let seconds: Double
}
protocol Timed {
var time: Time { get }
}
扩展定时协议添加只读计算的时间间隔属性
extension Timed {
var timeInterval: TimeInterval {
return TimeInterval(time.minutes * 60) + time.seconds
}
}
添加自定义初始化程序以延长时间:
extension Time {
init(time: TimeInterval) {
self.minutes = (Int(time) / 60) % 60
self.seconds = time.truncatingRemainder(dividingBy: 60)
}
}
然后你只需要将 Timed 协议添加到有时间的结构中 属性:
struct Root: Codable {
let myType: MyType
}
struct MyType: Codable, Timed {
let time: Time
}
测试:
let root = Root(myType: MyType(time: Time(time: 65.12)))
root.myType.timeInterval
do {
let encoded = try JSONEncoder().encode(root)
print(String(data: encoded, encoding: .utf8)!) // "{"myType":{"time":{"minutes":1,"seconds":5.1200000000000045}}}\n
} catch {
print(error)
}
在 Swift 4 中以自定义方式将自定义类型编码为 JSON 很容易,并且有很好的文档可用。但是我在以自定义方式编码 Foundation
类型时遇到了问题。我的属性类型为 TimeInterval
的自定义类型需要 Codable
,格式为 JSON,显示分钟和秒,如下所示:
// my type:
struct MyType: Codable {
let time: TimeInterval = 65.12
}
// required JSON representation:
"myType": {
"time":
{
"minutes": 1,
"seconds": 5.12
}
}
如果我为我的每个类型提供 encode(to:)
和 init(decoder:)
的自定义实现,这很好用。但我不想重复我的话,只是为 TimeInterval
提供这些方法的自定义版本。所以我尝试了这样的扩展:
extension TimeInterval {
enum CodingKeys: String, CodingKey {
case minutes
case seconds
}
func encode(to encoder: Encoder) throws {
let minutes = (NSInteger(self) / 60) % 60
let seconds = self.truncatingRemainder(dividingBy: 60)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(minutes, forKey: .minutes)
try container.encode(seconds, forKey: .seconds)
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
guard let minutes = try? values.decode(Int.self, forKey: .minutes), let seconds = try? values.decode(Double.self, forKey: .seconds) else {
throw NSError()
}
self = TimeInterval(Double(minutes) * 60.0 + seconds)
}
}
不幸的是,此代码永远不会被调用:编码 TimeInterval
值似乎总是使用编码 Double
的默认实现,它符合开箱即用的 Codable
。
let myType = MyType(time: 65.12)
let encoded = try! JSONEncoder().encode(myType)
let json = String(data: encoded, encoding: .utf8)!
print(json)
// prints:
// "myType":
// {
// "time": 65.12
// }
我该如何解决这个问题?
编辑:
就像我在第一句话中写的那样,我不是在寻找解决方案,如何编码我自己的自定义类型。这在我的代码中已经运行良好。我正在尝试 更改 基础类型 TimeInterval
的编码行为。
我修改了您的代码(仅作为示例),并且有效。
struct MyType {
let time: TimeInterval = 65.12
enum CodingKeys: String, CodingKey {
case minutes
case seconds
}
}
extension MyType: Encodable {
func encode(to encoder: Encoder) throws {
let minutes = 20
let seconds = 30
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(minutes, forKey: .minutes)
try container.encode(seconds, forKey: .seconds)
}
}
let myType = MyType()
let encoded = try! JSONEncoder().encode(myType)
let json = String(data: encoded, encoding: .utf8)!
print(json)
enum MyCodingKeys: String, CodingKey {
case minutes
case seconds
}
protocol TimeIntervalEncodable: Encodable {
var time: TimeInterval {get set}
}
extension TimeIntervalEncodable {
func encode(to encoder: Encoder) throws {
let minutes = (Int(time) / 60) % 60
let seconds = time.truncatingRemainder(dividingBy: 60)
var container = encoder.container(keyedBy: MyCodingKeys.self)
try container.encode(minutes, forKey: .minutes)
try container.encode(seconds, forKey: .seconds)
}
}
struct MyType: TimeIntervalEncodable {
var time: TimeInterval = 65.12
}
let myType = MyType()
let encoded = try! JSONEncoder().encode(myType)
let json = String(data: encoded, encoding: .utf8)!
print(json)
您可以将您的时间间隔设为只读计算 属性 并添加一个采用 TimeInterval 的自定义初始化程序并初始化您的时间子结构属性:
struct Root: Codable {
let myType: MyType
}
struct MyType: Codable {
let time: Time
struct Time: Codable {
let minutes: Int
let seconds: Double
}
init(time: TimeInterval) {
self.time = Time(minutes: (Int(time) / 60) % 60, seconds: time.truncatingRemainder(dividingBy: 60))
}
var timeInterval: TimeInterval {
return TimeInterval(time.minutes * 60) + time.seconds
}
}
let root = Root(myType: MyType(time: 65.12))
do {
let encoded = try JSONEncoder().encode(root)
print(String(data: encoded, encoding: .utf8)!) // "{"myType":{"time":{"minutes":1,"seconds":5.1200000000000045}}}\n
} catch {
print(error)
}
edit/update:
如果您需要将时间 属性 添加到多个结构中,您可以将结构时间从自定义类型中移出并创建一个定时协议:
struct Time: Codable {
let minutes: Int
let seconds: Double
}
protocol Timed {
var time: Time { get }
}
扩展定时协议添加只读计算的时间间隔属性
extension Timed {
var timeInterval: TimeInterval {
return TimeInterval(time.minutes * 60) + time.seconds
}
}
添加自定义初始化程序以延长时间:
extension Time {
init(time: TimeInterval) {
self.minutes = (Int(time) / 60) % 60
self.seconds = time.truncatingRemainder(dividingBy: 60)
}
}
然后你只需要将 Timed 协议添加到有时间的结构中 属性:
struct Root: Codable {
let myType: MyType
}
struct MyType: Codable, Timed {
let time: Time
}
测试:
let root = Root(myType: MyType(time: Time(time: 65.12)))
root.myType.timeInterval
do {
let encoded = try JSONEncoder().encode(root)
print(String(data: encoded, encoding: .utf8)!) // "{"myType":{"time":{"minutes":1,"seconds":5.1200000000000045}}}\n
} catch {
print(error)
}