【问题标题】:How to encode struct in Swift using Encodable that contains an already encoded value如何使用包含已编码值的 Encodable 在 Swift 中编码结构
【发布时间】:2019-09-30 21:52:29
【问题描述】:

想象一个如下的数据结构,在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

【问题讨论】:

  • 你可以更明确你需要什么
  • 刚刚添加了一些说明????

标签: json swift encoding codable encodable


【解决方案1】:

我同意 Ahmad 的基本方法,但我假设您需要一些更有活力的东西。在这种情况下,您应该明确content 不是“字符串”。它是 JSON。因此,您可以使用 JSON type 将其存储为 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() 就是你需要得到你描述的输出。

【讨论】:

  • 我明白了,谢谢。这在概念上类似于 Go 中的 json.RawMessage,它只是一个字节数组并假设 JSON is 正确。这基本上就是我希望在 Swift 中复制的内容。
  • 这让我想知道是否有可能已经代表 Data (typealias?) 并假设其值 is 正确的自定义?
  • 当然,但不是 JSONEncoder。你必须自己建造一些东西。您可能可以使用 JSONEncoder 来完成各个部分,然后将位粘合在一起,但无法直接访问输出字节。
【解决方案2】:

你可以这样做:

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

输出应该是:

{"other":{"foo":1},"contents":{"foo":"Foo","bar":1}}

【讨论】:

  • 我想“按原样”使用partial,因为解码会或多或少地使内存使用量翻倍并花费额外的时间。在我的情况下,这将是有问题的,因为我正在处理一个相当大的 JSON 数据结构。
  • 使用 JSONEncoder 是不可能的。在这种情况下,你必须从头开始构建整个东西。没有机制可以将未经验证的字符注入流中。按照你的建议做,你可能会得到无效的 JSON,而 JSONEncoder 不允许这样做。我建议对“rest”进行编码,然后根据您的需要使用标准字符串操作将两者粘合在一起。
  • 谢谢@RobNapier。您能否解释一下为什么不能使用 JSONEncoder?
  • 因为 JSONEncoder 不能直接访问输出字节流,所以没有地方可以写入原始字节。
【解决方案3】:

我可能有点晚了,但我希望它对未来的人有所帮助。我有一个类似的问题,我有一些预编码的变量,并希望将它们嵌套在一些可编码的父结构中。

struct Request: Encodable {
    let variables: [String: Data] // I'd encode data to JSON someplace else.
}

不幸的是,每个键值的类型各不相同(例如,一个键中可以有一个整数,另一个键中可以有一个对象),我无法从第一次编码的地方向上传递信息。这是我的想法:

{ 
    "variables": {
        "one": { "hello": "world" },
        "two": 2
    }
}

枚举和泛型也不是一个选项,因为这是一个高度灵活的部分,只需要符合 Encodable 的类型。

总而言之,我最终复制了大部分 Swift 的 JSONEncoder 实现,你可以找到 here。 (我建议清除 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
}

一旦你有了它,你可以将NSObject作为一个类型传递到任何地方,剩下的重要部分是你运行JSONSerialization.data函数来获取实际的JSON。 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 = httpBody

【讨论】:

    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    相关资源
    最近更新 更多