Search code examples
swiftcodabledecodable

How to remove data model nil fields from custom encoded/decoded json in Swift


I am trying to find a clean way to remove data model optional attributes if its nil when custom Encoding/Decoding my data model in Swift.

My use case:

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)")

I am getting the following json when encoding my model

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

However, I would like to remove null values from my json for nil values.

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

Solution

  • I found that decodeIfPresent, encodeIfPresent are used for this use case.

    try? is no longer used as well to validate those fields if they're present.

    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)
        }
    }