使用 Codable to encode/decode 从字符串到整数,中间有一个函数
Using Codable to encode/decode from Strings to Ints with a function in between
我有这个 json 字符串:
let json = """
{
"name": "Wendy Carlos",
"hexA": "7AE147AF",
"hexB": "851EB851"
}
"""
let data = Data(json.utf8)
...我想使用 Codable 将其编码到这个结构(并从中返回):
struct CodeMe: Codable {
var name: String
var hexA: Int
var hexB: Int
enum CodingKeys: String, CodingKey {
case name, hexA, hexB
}
}
let encoder = JSONEncoder()
let decoder = JSONDecoder()
但是 hexA 和 hexB 是字符串(在 JSON 中),我需要它们是 Swift 对象中的整数。为此,我已经编写了 20 个函数。例如在伪代码中:
func hexAConversion(from hex: String)->Int {
// returns an Int between -50 and 50
}
func hexBConversion(from hex: String)->Int {
// returns an Int between 0 and 360
}
考虑到像这样的转换方案很少,我需要再编写 20 个函数(用于 Int->Hexadecimal 往返),我将如何编写自定义解码 和 编码策略适用于上述?
我看过这些解决方案: 但我的用例似乎略有不同,因为接受的答案看起来像是处理直接类型转换,而我需要 运行一些功能。
对于Codable
需要自定义转换类型东西的编码和解码,你只需要自己实现初始化和编码方法。在您的情况下,它看起来像这样。只是为了真正清楚地表达这个想法有点冗长。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
let hexAString: String = try container.decode(String.self, forKey: .hexA)
self.hexA = hexAConversion(from: hexAString)
let hexBString: String = try container.decode(String.self, forKey: .hexB)
self.hexB = hexBConversion(from: hexBString)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
//Assuming you have another set of methods for converting back to a String for encoding
try container.encode(self.name, forKey: .name)
try container.encode(hexAStringConversion(from: self.hexA), forKey: .hexA)
try container.encode(hexBStringConversion(from: self.hexB), forKey: .hexB)
}
我有这个 json 字符串:
let json = """
{
"name": "Wendy Carlos",
"hexA": "7AE147AF",
"hexB": "851EB851"
}
"""
let data = Data(json.utf8)
...我想使用 Codable 将其编码到这个结构(并从中返回):
struct CodeMe: Codable {
var name: String
var hexA: Int
var hexB: Int
enum CodingKeys: String, CodingKey {
case name, hexA, hexB
}
}
let encoder = JSONEncoder()
let decoder = JSONDecoder()
但是 hexA 和 hexB 是字符串(在 JSON 中),我需要它们是 Swift 对象中的整数。为此,我已经编写了 20 个函数。例如在伪代码中:
func hexAConversion(from hex: String)->Int {
// returns an Int between -50 and 50
}
func hexBConversion(from hex: String)->Int {
// returns an Int between 0 and 360
}
考虑到像这样的转换方案很少,我需要再编写 20 个函数(用于 Int->Hexadecimal 往返),我将如何编写自定义解码 和 编码策略适用于上述?
我看过这些解决方案:
对于Codable
需要自定义转换类型东西的编码和解码,你只需要自己实现初始化和编码方法。在您的情况下,它看起来像这样。只是为了真正清楚地表达这个想法有点冗长。
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
let hexAString: String = try container.decode(String.self, forKey: .hexA)
self.hexA = hexAConversion(from: hexAString)
let hexBString: String = try container.decode(String.self, forKey: .hexB)
self.hexB = hexBConversion(from: hexBString)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
//Assuming you have another set of methods for converting back to a String for encoding
try container.encode(self.name, forKey: .name)
try container.encode(hexAStringConversion(from: self.hexA), forKey: .hexA)
try container.encode(hexBStringConversion(from: self.hexB), forKey: .hexB)
}