Swift Codable - 如何编码和解码字符串化的 JSON 值?
Swift Codable - how to encode and decode stringified JSON values?
与我通话的服务器需要格式为以下格式的消息:
{
"command": "subscribe",
"identifier": "{\"channel\": \"UserChannel\"}",
"data": "{\"key\": \"value\"}"
}
其中 identifier
和 data
值是转义的 json 字符串。
到目前为止我有这个:
struct ActionCableMessage<Message: Encodable>: Encodable {
let command: Command
let identifier: CableChannel
let data: Message?
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(command, forKey: .command)
try container.encode(identifier, forKey: .identifier) // ????
}
private enum CodingKeys: String, CodingKey {
case command, identifier, data
}
}
但我不知道从这里可以做什么。我想我需要一个 CableChannel
和 Message
可以符合的 protocol
,并提供一个实现 encode (to encoder: Encoder)
的 extension
函数,确保 Encoder
必须是 JSONEncoder
,如果是,则使用它重写它自己的值作为转义的 json 字符串。
我还需要将其解码回 ActionCableMessage
结构,但我还没有做到这一点。
I think I need a protocol that CableChannel and Message can conform to
嗯,那个协议是 Encodable
(或者 Codable
,如果你愿意的话)。
// just implement these as you normally would
extension CableChannel : Encodable { ... }
extension Message : Encodable { ... }
然后在 ActionCableMessage
中,您使用另一个编码器将内部对象编码为 JSON 数据,然后将其转换为字符串,然后对该字符串进行编码:
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(command, forKey: .command)
let subencoder = JSONEncoder()
let identifierString = try String(data: subencoder.encode(identifier), encoding: .utf8)
try container.encode(identifierString, forKey: .identifier)
// do the same for "data"
}
解码类似:
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
command = try container.decode(String.self, forKey: .command)
let identifierJSONString = try container.decode(String.self, forKey: .identifier)
// do the same for "data"
let subdecoder = JSONDecoder()
identifier = try subdecoder.decode(CableChannel.self, from: identifierJSONString.data(using: .utf8)!)
// do the same for "data"
}
与我通话的服务器需要格式为以下格式的消息:
{
"command": "subscribe",
"identifier": "{\"channel\": \"UserChannel\"}",
"data": "{\"key\": \"value\"}"
}
其中 identifier
和 data
值是转义的 json 字符串。
到目前为止我有这个:
struct ActionCableMessage<Message: Encodable>: Encodable {
let command: Command
let identifier: CableChannel
let data: Message?
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(command, forKey: .command)
try container.encode(identifier, forKey: .identifier) // ????
}
private enum CodingKeys: String, CodingKey {
case command, identifier, data
}
}
但我不知道从这里可以做什么。我想我需要一个 CableChannel
和 Message
可以符合的 protocol
,并提供一个实现 encode (to encoder: Encoder)
的 extension
函数,确保 Encoder
必须是 JSONEncoder
,如果是,则使用它重写它自己的值作为转义的 json 字符串。
我还需要将其解码回 ActionCableMessage
结构,但我还没有做到这一点。
I think I need a protocol that CableChannel and Message can conform to
嗯,那个协议是 Encodable
(或者 Codable
,如果你愿意的话)。
// just implement these as you normally would
extension CableChannel : Encodable { ... }
extension Message : Encodable { ... }
然后在 ActionCableMessage
中,您使用另一个编码器将内部对象编码为 JSON 数据,然后将其转换为字符串,然后对该字符串进行编码:
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(command, forKey: .command)
let subencoder = JSONEncoder()
let identifierString = try String(data: subencoder.encode(identifier), encoding: .utf8)
try container.encode(identifierString, forKey: .identifier)
// do the same for "data"
}
解码类似:
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
command = try container.decode(String.self, forKey: .command)
let identifierJSONString = try container.decode(String.self, forKey: .identifier)
// do the same for "data"
let subdecoder = JSONDecoder()
identifier = try subdecoder.decode(CableChannel.self, from: identifierJSONString.data(using: .utf8)!)
// do the same for "data"
}