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: Unmarshal does not handle structs with lowercase fields #8598

Closed
gopherbot opened this issue Aug 26, 2014 · 4 comments

Comments

Projects
None yet
3 participants
@gopherbot
Copy link

commented Aug 26, 2014

by ginabythebay:

What does 'go version' print? 1.3

What steps reproduce the problem?
If possible, include a link to a program on play.golang.org.

1. Run this: http://play.golang.org/p/EXzXy8Ec01

What happened? There is no error and the struct has no fields set

What should have happened instead? The name field should have been set to foo.

Please provide any additional information below.

Apparently Unmarshal silently ignores field names that are lowercase.  If I change the
'name' field in the struct to 'Name', then it works.  This is especially painful as
examples in effective go (e.g. https://golang.org/doc/effective_go.html#formatting) show
structs with lowercase fields.  Ideally, Unmarshal would handle structs with lowercase
fields.  If that isn't practical for some reason, the docs should mention it, and
perhaps Unmarshal should return an error if passed a struct type which has no fields
that are settable.
@minux

This comment has been minimized.

Copy link
Member

commented Aug 26, 2014

Comment 1:

lower-cased fields are not exported, so encoding/json (reflect)
can't set it.
the proper way to go is export that field and then use struct tags.

Status changed to WorkingAsIntended.

@gopherbot

This comment has been minimized.

Copy link
Author

commented Aug 26, 2014

Comment 2 by ginabythebay:

Well, perhaps (as I said originally) the docs should mention this and perhaps Unmarshal
could return an error if passed a struct with no exported fields.
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Aug 26, 2014

Comment 3:

Technically, I think it is documented.  The Marshal docs say "Each exported struct field
becomes a member of the object."  The Unmarshal docs say "Unmarshal uses the inverse of
the encodings that Marshal uses."
I wouldn't object to another sentence in the Unmarshal docs, though.
@minux

This comment has been minimized.

Copy link
Member

commented Sep 6, 2014

Comment 4:

Issue #8671 has been merged into this issue.

@golang golang locked and limited conversation to collaborators Jun 25, 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.