json - 如何使用包含已编码值的 Encodable 在 Swift 中编码结构
问题描述
想象一个如下的数据结构,其中包含contents
一个已编码的 JSON 片段的值。
let partial = """
{ "foo": "Foo", "bar": 1 }
"""
struct Document {
let contents: String
let other: [String: Int]
}
let doc = Document(contents: partial, other: ["foo": 1])
期望的输出
组合数据结构应按contents
原样使用并进行编码other
。
{
"contents": { "foo": "Foo", "bar": 1 },
"other": { "foo": 1 }
}
使用Encodable
以下实现Encodable
编码Document
为 JSON,但它也重新编码contents
为字符串,这意味着它被包裹在引号中,并且所有"
引号都转义为\"
.
extension Document : Encodable {
enum CodingKeys : String, CodingKey {
case contents
case other
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(contents, forKey: .contents)
try container.encode(other, forKey: .other)
}
}
输出
{
"contents": "{\"foo\": \"Foo\", \"bar\": 1}",
"other": { "foo": 1 }
}
怎么可能encode
就这样通过contents
?
解决方案
我同意艾哈迈德的基本方法,但我假设你需要一些更有活力的东西。在这种情况下,您应该明确这content
不是“字符串”。它是 JSON。因此,您可以使用JSON 类型将其存储为 JSON (在此处进行了简化,请参阅 gist 以获取功能更丰富的版本):
enum JSON: Codable {
struct Key: CodingKey, Hashable, CustomStringConvertible {
var description: String {
return stringValue
}
let stringValue: String
init(_ string: String) { self.stringValue = string }
init?(stringValue: String) { self.init(stringValue) }
var intValue: Int? { return nil }
init?(intValue: Int) { return nil }
}
case string(String)
case number(Double) // FIXME: Split Int and Double
case object([Key: JSON])
case array([JSON])
case bool(Bool)
case null
init(from decoder: Decoder) throws {
if let string = try? decoder.singleValueContainer().decode(String.self) { self = .string(string) }
else if let number = try? decoder.singleValueContainer().decode(Double.self) { self = .number(number) }
else if let object = try? decoder.container(keyedBy: Key.self) {
var result: [Key: JSON] = [:]
for key in object.allKeys {
result[key] = (try? object.decode(JSON.self, forKey: key)) ?? .null
}
self = .object(result)
}
else if var array = try? decoder.unkeyedContainer() {
var result: [JSON] = []
for _ in 0..<(array.count ?? 0) {
result.append(try array.decode(JSON.self))
}
self = .array(result)
}
else if let bool = try? decoder.singleValueContainer().decode(Bool.self) { self = .bool(bool) }
else if let isNull = try? decoder.singleValueContainer().decodeNil(), isNull { self = .null }
else { throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [],
debugDescription: "Unknown JSON type")) }
}
func encode(to encoder: Encoder) throws {
switch self {
case .string(let string):
var container = encoder.singleValueContainer()
try container.encode(string)
case .number(let number):
var container = encoder.singleValueContainer()
try container.encode(number)
case .bool(let bool):
var container = encoder.singleValueContainer()
try container.encode(bool)
case .object(let object):
var container = encoder.container(keyedBy: Key.self)
for (key, value) in object {
try container.encode(value, forKey: key)
}
case .array(let array):
var container = encoder.unkeyedContainer()
for value in array {
try container.encode(value)
}
case .null:
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}
}
有了它,您可以重新定义您的文档以保存 JSON:
struct Document: Codable {
let contents: JSON
let other: [String: Int]
}
如果您愿意,可以从字符串中解码 JSON:
let doc = Document(contents:
try! JSONDecoder().decode(JSON.self, from: Data(partial.utf8)),
other: ["foo": 1])
有了它,默认值JSONEncoder()
就是您获取所描述的输出所需的全部内容。
推荐阅读
- logic - 如何将一系列逻辑表达式转换/形式化为可以提供给 DPLL 算法的格式?
- laravel - 如何在 laravel 中将 Base64 字符串保存为图像
- python - raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: for send whatsapp to all conatcts
- ffmpeg - ffmpeg 将 hdmv pgs 字幕从 mkv 提取到 srt
- reactjs - 非材质ui组件中明暗主题切换
- python - 为什么图形窗口打不开?
- glsl - 优化片段着色器上的复杂 Mobius 变换
- ansible - 如何在ansible主机中以root用户身份执行命令
- charts - 在 pinescript 中获取主图表系列的会话时间(开始和结束时间)
- java - 删除后列表不更新