Skip to content

Provide Decoder to turn JSON into Decodable #34

Description

@aomader

Hej, it is nice to have JSON as an intermediate correctly-typed abstraction over same JSON data, but at some point I might still want to work with some dedicated data class. However, at that point I might only have the JSON left and I can not make use of some Decoder to turn my JSON into an appropriate Decodable for the task at hand.

I have a concret use case for that, where the concrete structure of my JSON is only clear at a later point in time.

Thus, I would suggest to provide a GenericJSONDecoder to turn a JSON into a Decodable. Analogous to JSONDecoder and Data.

The solution proposed here solves the issue in the meantime, but is really wasteful and rather just a short-time fix:

func decode<T: Decodable>(from json: JSON) throws -> T {
    let data = try JSONEncoder().encode(json)
    return try JSONDecoder().decode(T.self, from: data)
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions