Skip to content

proposal: encoding/json: Unmarshal support for dynamic types #13338

Closed
@anpez

Description

@anpez

I'd love to see a version of json.Unmarshal that accepts a type selector. E.g. a function that, given the to-be-decoded json returns the type that should be unmarshalled.

This is very useful if you are reading a JSON array where the elements don't all have the same fields and you are ok with a heterogeneous array.

Say you have this JSON:

[{"a":1}, {"b":2}]

and you want the unmarshalled array to be like this

[]interface{}{A{A: 1}, B{B: 2}}

you could pass in a function to Unmarshal that, given the map[string]interface{} containing every one of the elements would return an empty struct to be unmarshalled to. These would, of course, slow the things down as it would require double unmarshalling.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions