设想一个如下的数据结构,其中包含一个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?
发布于 2019-10-01 06:22:54
我同意Ahmad的基本方法,但我假设你需要一些更有活力的东西。在这种情况下,您应该明确指出content不是“字符串”。这是JSON。因此,您可以使用JSON type将其存储为JSON (这里进行了简化,有关功能更丰富的版本,请参阅要点):
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()就是获得所描述的输出所需的全部。
发布于 2019-10-01 06:12:27
你可以通过这样做来实现它:
let partial = """
{
"foo": "Foo",
"bar": 1
}
"""
// declare a new type for `content` to deal with it as an object instead of a string
struct Document {
let contents: Contents
let other: [String: Int]
struct Contents: Codable {
let foo: String
let bar: Int
}
}
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)
}
}
let decoder = JSONDecoder()
let contents = try decoder.decode(Document.Contents.self, from: partial.data(using: .utf8)!)
let encoder = JSONEncoder()
let doc = Document(contents: contents, other: ["foo": 1])
let result = try encoder.encode(doc)
print(String(data: result, encoding: .utf8)!)基本上,您可以首先通过解码来处理partial,然后将解码结果传递给Document。
输出应为:
{“其他”:{“Foo”:1},"contents":{"foo":"Foo",“”:1}}
发布于 2020-10-28 04:35:12
我可能会晚一点,但我希望这对未来的人有帮助。我遇到过类似的问题,我有一些预编码的变量,并希望将它们嵌套在某个可编码的父结构中。
struct Request: Encodable {
let variables: [String: Data] // I'd encode data to JSON someplace else.
}不幸的是,每个键值的类型是不同的(例如,你可以在一个键中有一个整数,在另一个键中有一个对象),并且我不能从我第一次编码它的地方向上传递信息。下面是我的想法:
{
"variables": {
"one": { "hello": "world" },
"two": 2
}
}枚举和泛型也不是一个选择,因为这是一个高度灵活的部分,只需要类型符合Encodable。
总而言之,我最终复制了您可以找到here的大部分Swift的JSONEncoder实现。(我建议清理JSONDecoder实现,因为它在我们的例子中没有用处。)
需要更改的部分在JSONEncoder类的encode函数中。基本上,您希望将获取topLevel值的部分(即NSObject)和序列化它的部分分开。新的encode还应该返回NSObject-type,而不是Data。
open func encode<T : Encodable>(_ value: T) throws -> NSObject {
let encoder = __JSONEncoder(options: self.options)
guard let topLevel = try encoder.box_(value) else {
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) did not encode any values."))
}
return topLevel
}一旦你有了它,你就可以将JSON作为一个类型来传递,剩下的重要一步就是你运行JSONSerialization.data函数来获得实际的NSObject。JSONEncoder在内部所做的是将Encodable结构简化为Foundation类型。然后,JSONSerialization可以处理这些类型,您将获得一个有效的JSON。
下面是我如何使用它的:
let body: Any = [
"query": query, // String
"variables": variables // NSObject dictionary
]
let httpBody = try! JSONSerialization.data(
withJSONObject: body,
options: JSONSerialization.WritingOptions()
)
request.httpBody = httpBodyhttps://stackoverflow.com/questions/58175721
复制相似问题