0

Im working on a JSON payload with my object payload. but im having trouble encoding object inside object.

My Payload class was this

class ConversationPayload :BaseObject {
    var title : String? = ""
    var messageDict: MessagePayload = MessagePayload()
    var participants: [Int32] = []
    var type: String = ""
    
    enum CodingKeys: String, CodingKey {
        case title = "title"
        case messageDict = "message"
        case participants = "participants"
        case type = "type"
    }

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        if title != nil {
            try container.encode(title, forKey: .title)
        }
        try container.encode(messageDict, forKey: .messageDict)
        try container.encode(participants, forKey: .participants)
        try container.encode(type, forKey: .type)
    }
    
}

class MessagePayload: BaseObject {
    var body : String = ""
    var isVideocallInvite: Bool = false
    var attachmentsPayload: MessageAttachmentPayload? = nil
    
    enum CodingKeys: String, CodingKey {
        case body = "body"
        case isVideocallInvite = "is_videocall_invite"
        case attachmentsPayload = "attachment"
    }

    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(body, forKey: .body)
        try container.encode(isVideocallInvite, forKey: .isVideocallInvite)
        if attachmentsPayload != nil {
            try container.encode(attachmentsPayload, forKey: .attachmentsPayload)
        }
        
    }
}

class MessageAttachmentPayload: BaseObject {
    var photo : String = ""
    var photoType : String = "jpg"
}

BaseObject was this

class BaseObject:Codable{}

What i want to get in json payload was something like this

{"message": {"body": "body_string", "is_videocall_invite": 1}, "participants" : [user-id], "type" : "converstation_type","title":"title"}

anyone know whats wrong with my payload class? not that familiar yet on codable. thanks in advance.

Edgar
  • 179
  • 11

2 Answers2

1

I'm not sure what constrains you have here, but I would simplify all of this down. Keep the JSON data models as close to the JSON as you can get.

struct ConversationJsonModel: Codable {
    var title: String?
    var message: MessageJsonModel
    var participants: [Int]
    var type: String
}

struct MessageJsonModel: Codable {
    var body: String
    var is_videocall_invite: Int
    var attachment: AttachmentJsonModel?
}

struct AttachmentJsonModel: Codable {
    var photo: String
    var photo_type: String // <-- assuming photo_type is the JSON member name.
}

If you need a view model or some other kind of local data model, then the two parts are separate.

class BaseObject {}

class ConversationPayload: BaseObject {
    var title : String? = ""
    var messageDict: MessagePayload = MessagePayload()
    var participants: [Int32] = []
    var type: String = ""

    func makeConversationJsonModel() -> ConversationJsonModel {
        ConversationJsonModel(title: title,
                              message: messageDict.makeMessageJsonModel(),
                              participants: participants.map { Int($0) },
                              type: type)
    }
}

class MessagePayload: BaseObject {
    var body : String = ""
    var isVideocallInvite: Bool = false
    var attachmentsPayload: MessageAttachmentPayload? = nil

    func makeMessageJsonModel() -> MessageJsonModel {
        MessageJsonModel(body: body,
                         is_videocall_invite: isVideocallInvite ? 1 : 0,
                         attachment: attachmentsPayload?.makeAttachmentJsonModel())
    }
}

class MessageAttachmentPayload: BaseObject {
    var photo : String = ""
    var photoType : String = "jpg"

    func makeAttachmentJsonModel() -> AttachmentJsonModel {
        AttachmentJsonModel(photo: photo, photo_type: photoType)
    }
}

Finally, encoding your JSON

let conversationPayload = ConversationPayload()
let json = try? JSONEncoder().encode(conversationPayload.makeConversationJsonModel())

This allows for clean separation between the JSON representation and the payload model. For example, in the JSON, is_videocall_invite is an Int (0 or 1); meanwhile, in the payload model, isVideocallInvite is a Bool.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
0

What is the issue? I just tested your class, it has some minor issues but it does conforming to codable and it encoded without issue.

In playground:

var conversation = ConversationPayload()
var message = MessagePayload()
message.body = "message body"
message.isVideocallInvite = true
var attachment = MessageAttachmentPayload()
attachment.photo = "attachment_file"
message.attachmentsPayload = attachment
conversation.messageDict = message
conversation.title = "Title"
conversation.participants = [1, 2, 3, 4]
conversation.type = "Conversation Type"

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try! encoder.encode(conversation)
print(String(data: data, encoding: .utf8)!)

The output looks like the following:

{
  "participants" : [
    1,
    2,
    3,
    4
  ],
  "message" : {
    "is_videocall_invite" : true,
    "attachment" : {
      "photo" : "attachment_file",
      "photoType" : "jpg"
    },
    "body" : "message body"
  },
  "title" : "Title",
  "type" : "Conversation Type"
}

I have added encode and CodingKey in MessageAttachmentPayload class to encode the values.

Isn't it what you were expecting?

andykkt
  • 1,696
  • 16
  • 23
  • i saw the error. the API needs a integer (1,0) on is_videocall_invite so i change it from bool to int. thanks for the response – Edgar Nov 16 '20 at 04:53