Serializing Foundation and non-foundation types to JSON in swift

125 views Asked by At

I'm trying to serialize two different types of object to JSON, but I've only been able to do so using JSONEncoder for one, and JSONSerialization for the other.

One is a simple Int, which I serialize as follows:

let ticket: [String:Any] = ["ticket": -1]
let ticketJson = try! JSONSerialization.data(withJSONObject: ticket)
let ticketJsonString = String(data: ticketJson, encoding: String.Encoding.utf8)

The other object type is a class I created, which I serialize as follows:

let encodedNotification = try! JSONEncoder().encode(notification)
let encodedNotificationJsonString = String(data: encodedNotification, encoding: String.Encoding.utf8)

Ideally, I would serialize both at once, using the following dictionary:

let parameters: [String:Any] = ["ticket": -1, "notification": notification]

Is this possible? If so, how? I have not been able to figure it out.

Thanks!

1

There are 1 answers

2
Rob Napier On BEST ANSWER

Put the values in a struct rather than a [String: Any].

struct Parameters: Encodable {
    let ticket: Int
    let notification: Notification
}

let parameters = Parameters(ticket: -1, notification: notification)

let encoded = try JSONEncoder().encode(parameters)

As a rule, avoid Any in Swift. It is a very difficult type to use correctly and does not play particularly nicely with the type system. Except in fairly advanced code, it mostly shows up due to ObjC-bridging (such as in JSONSerialization, which is a bridge to the ObjC NSJSONSerialization).