gpt4 book ai didi

json - 如何以自定义方式对已经符合 Codable 的基础类型(例如 TimeInterval)进行编码?

转载 作者:搜寻专家 更新时间:2023-10-31 22:25:53 24 4
gpt4 key购买 nike

在 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 的编码行为。

最佳答案

您可以将您的时间间隔设置为只读计算属性,并添加一个采用 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)
}

编辑/更新:

如果您需要将时间属性添加到多个结构中,您可以将结构时间从自定义类型中移出并创建一个时间协议(protocol):

struct Time: Codable {
let minutes: Int
let seconds: Double
}
protocol Timed {
var time: Time { get }
}

扩展 Timed 协议(protocol),添加 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)
}
}

然后您只需要将 Timed 协议(protocol)添加到具有时间属性的结构中:

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)
}

关于json - 如何以自定义方式对已经符合 Codable 的基础类型(例如 TimeInterval)进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47760983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com