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
Closed

Comments

@gopherbot
Copy link

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
Copy link
Member

minux 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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Member

minux commented Sep 6, 2014

Comment 4:

Issue #8671 has been merged into this issue.

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants