![](/img/trans.png)
[英]How to encode a property with type of JSON dictionary in Swift 4 encodable protocol
[英]How to encode struct in Swift using Encodable that contains an already encoded value
想象一個如下的數據結構,在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 (在此處進行了簡化,請參閱要點以獲得更豐富的版本):
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()
就是你所描述的 output 所需要的一切。
你可以通過這樣做來實現它:
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
。
output 應該是:
{"other":{"foo":1},"contents":{"foo":"Foo","bar":1}}
我可能有點晚了,但我希望它對未來的人有所幫助。 我有一個類似的問題,我有一些預編碼的變量,並想將它們嵌套在一些可編碼的父結構中。
struct Request: Encodable {
let variables: [String: Data] // I'd encode data to JSON someplace else.
}
不幸的是,每個鍵值的類型各不相同(例如,您可以在一個鍵中使用 integer,在另一個鍵中使用 object)並且我無法從第一次對其進行編碼的位置向上傳遞信息。 這是我的想法:
{
"variables": {
"one": { "hello": "world" },
"two": 2
}
}
Enum 和 Generics 也不是一個選項,因為這是一個高度靈活的部分,只需要符合 Encodable 的類型。
總而言之,我最終復制了您可以在此處找到的大部分 Swift JSONEncoder 實現。 (我建議清除 JSONDecoder 實現,因為它在我們的案例中沒有用。)
需要更改的部分是 JSONEncoder class 中的encode
JSONEncoder
內部。 基本上,您想要拆分獲得topLevel
值的部分(即NSObject
)和序列化它的部分。 新的encode
還應該返回一個NSObject
類型而不是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
function 來獲得實際的 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.