如何在Swift

时间:2019-11-11 23:12:26

标签: swift codable decodable

在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值的空值。

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

1 个答案:

答案 0 :(得分:3)

我发现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)
    }
}