Skip to content

encoding/json: encode map key is of string kind, decode only of string type #3519

Closed
@gopherbot

Description

@gopherbot

by acmmlyc:

There's an asymmetry that's bugging me—I don't know if you care enough to have it
fixed, but it tripped me up, so it may be worth.

Encoding a map fails if v.Type().Key().Kind() != reflect.String, meaning the string type
*and* any synonym thereof (constructed with type X string) will work as a key.

Conversely, decoding fails if t := v.Type(); t.Key() != reflect.TypeOf(""),
forcing a string type rather than string kind.

Small demonstration here: http://play.golang.org/p/Jcl_elUQNx

Encode produces sensible JSON, but decode doesn't mutate the map.

Ideally it'd be consistent one way or another; by encoding rejecting anything not a
"string" type, or decoding accepting non-"string"-typed
string-kinds! If you think this is sensible, I'd be happy to try to hack a patch
together.

Cheers,

Arlen

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeSuggestedIssues that may be good for new contributors looking for work to do.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions