Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gopherbot opened this issue Apr 13, 2012 · 6 comments

Comments

Projects
None yet
3 participants
@gopherbot
Copy link

commented Apr 13, 2012

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
@rsc

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2012

Comment 1:

Labels changed: added priority-later, removed priority-triage.

Status changed to Accepted.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2012

Comment 2:

Labels changed: added go1.1.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 10, 2012

Comment 3:

Labels changed: added size-m.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Dec 10, 2012

Comment 4:

Labels changed: added suggested.

@davecheney

This comment has been minimized.

Copy link
Contributor

commented Dec 30, 2012

Comment 5:

This issue was closed by revision a460012.

Status changed to Fixed.

@gopherbot

This comment has been minimized.

Copy link
Author

commented Dec 30, 2012

Comment 6 by ar@len.me:

Thanks!

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015

@rsc rsc removed the go1.1 label Apr 14, 2015

@golang golang locked and limited conversation to collaborators Jun 24, 2016

This issue was closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.