Skip to content

Decode responses to Struct via Codable #952

Open
@kristoff-it

Description

@kristoff-it

At the moment objects coming in via the socket get decoded as [String: Any], and to obtain a struct from a response one has to manually implement something along the lines of init(dict: dataComingFromSocketIO) in which every single property has to be manually mapped.

Some answers on Stack overflow even suggest to serialize the Dictionary back to json and use another library to parse it back into a Struct using the Codable protocol, which is... not optimal.

Considering that communications through the socket come encoded as json, it seems plausible to inject the same behavior during the initial decoding step using the standard json decoder.

The interface could look something like this:
func emit<U: Codable> (_ event: String, decodeAs: U, _ items: SocketData...)

Is there a reason why this might be harder than what it seems?
Or maybe is someone already working on that?

If it's not too involved I might try to start working on a PR, let me know please.
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions