gpt4 book ai didi

swift - 如何在 Swift 中从自定义编码/解码的 json 中删除数据模型 nil 字段

转载 作者:行者123 更新时间:2023-11-28 10:26:54 25 4
gpt4 key购买 nike

当在 Swift 中自定义编码/解码我的数据模型时,我正在尝试找到一种干净的方法来删除数据模型可选属性(如果其nil)。

我的用例:

import Foundation

public struct Message {
public let txnID: UUID
public var userId: String?
public var messageID: UUID?
public init(txnID: UUID, userId: String? = nil, messageID: UUID? = nil) {
self.txnID = txnID
self.userId = userId
self.messageID = messageID
}
}


extension Message: Codable {
private enum CodingKeys: CodingKey {
case txnID, userId, messageID
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
txnID = try container.decode(UUID.self, forKey: .txnID)
// FIXME: Remove `userId, messageID` if `nil`
self.userId = try? container.decode(String.self, forKey: .userId)
self.messageID = try? container.decode(UUID.self, forKey: .messageID)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.txnID, forKey: .txnID)
// FIXME: Remove `userId, messageID` if `nil`
try container.encode(self.userId, forKey: .userId)
try container.encode(self.messageID, forKey: .messageID)
}
}


/// The test case I have is basically is:
/// 1. Custom encode and decode my data model using `JSONEncoder` and `JSONDecoder`
/// 2. Remove optional attributes from the resulting encoded/decoded values

let msg = Message(txnID: UUID())
guard let encodedMsg = try? JSONEncoder().encode(msg), let jsonMessage = String(data: encodedMsg, encoding: String.Encoding.utf8) else {
fatalError()
}

// Now decode message
guard let origianlMsg = try? JSONDecoder().decode(Message.self, from: encodedMsg) else {
fatalError()
}

print("Encoded Message to json: \(jsonMessage)")

在对模型进行编码时,我得到以下 json

Encoded Message to json: {"txnID":"6211905C-8B72-4E19-81F0-F95F983F08CC","userId":null,"messageID":null}

但是,我想从 json 中删除 nil 值的 null 值。

Encoded Message to json: {"txnID":"50EFB999-C513-4DD0-BD3F-EEAE3F2304E9"}

最佳答案

我发现 decodeIfPresentencodeIfPresent 用于此用例。

try? 也不再用于验证这些字段(如果存在)。

extension Message: Codable {
private enum CodingKeys: CodingKey {
case txnID, userId, messageID
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
txnID = try container.decode(UUID.self, forKey: .txnID)
self.userId = try container.decodeIfPresent(String.self, forKey: .userId)
self.messageID = try container.decodeIfPresent(UUID.self, forKey: .messageID)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.txnID, forKey: .txnID)
try container.encodeIfPresent(self.userId, forKey: .userId)
try container.encodeIfPresent(self.messageID, forKey: .messageID)
}
}

关于swift - 如何在 Swift 中从自定义编码/解码的 json 中删除数据模型 nil 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58809868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com