以自定义方式编码自定义类型的JSON在Swift 4中很容易,并且有很好的文档可用。但是我在以自定义方式编码Foundation
类型时遇到了麻烦。我的自定义类型具有TimeInterval
类型的属性,需要用JSON格式为Codable
,显示出这样的分钟和几秒钟:
// 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
的编码行为。
您可以使时间间隔仅读取计算的属性,并添加一个定制初始化器,该属性需要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)
}
编辑/更新:
如果您需要将时间属性添加到更多的结构中,则可以将结构的时间移出自定义类型并创建定时协议:
struct Time: Codable {
let minutes: Int
let seconds: Double
}
protocol Timed {
var time: Time { get }
}
扩展了定时协议,添加了TimeInterval读取的唯一计算属性
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)
}
}
然后,您只需要将定时协议添加到具有时间属性的结构:
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)
}
我修改了您的代码(例如),这有效。
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)