-
Notifications
You must be signed in to change notification settings - Fork 18.1k
encoding/json Unmarshal function does not work with lowercase struct fields of go type #61335
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
Comments
I took the Unmarshal example from the library und slowly changed it to become closer to what I program --- a chess server, where I tried Unmarshal but where it never worked correctly. |
This is not a bug but a result of the fact that unexported identifiers are not visible to other packages. The https://go.dev/ref/spec#Exported_identifiers Did that make sense? |
I see what you mean and I almost anticipated it, but |
Thanks for looking at this. Do you know the proper place ? I looked at decode.go, but apart from "export" i have no hint what to look for. |
@ogeffert -- I should have given a more complete answer. You can annotate the your struct members to tell the decoder which JSON keys to map to it. For example, your
Please check out the JSON and Go blog post on the main site for details on the Did that solve your problem? |
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only. For questions please refer to https://github.com/golang/go/wiki/Questions |
@seankhliao This is not a question, sorry. |
@ayang64 I cannot see that annotation cures something here. This is a limitation that could be lifted by some boolean switch built into the library. |
@ogeffert This is documented at https://pkg.go.dev/encoding/json#Marshal, which says "Each exported struct field becomes a member of the object". The same applies to Unmarshal. The Marshal doc also describes how to set the JSON name when it should be distinct from the field name. You may find it helpful to read https://go.dev/blog/json. The difference between exported and unexported fields is a characteristic of the language itself, not something that can be addressed in the library. |
The language go is as it is. I like it. Will use the ojg package instead, as it is more liberal in its approach. Thanks to all of for your comments ! |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
package main
import (
"encoding/json"
"fmt"
)
func main() {
var bytes = []byte(
{"user": "magnus", "rating": "3856"}
)type Animal struct {
user string
rating string
}
var one_animal Animal
err := json.Unmarshal(bytes, &one_animal)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", one_animal)
}
What did you expect to see?
{User:magnus Rating:3856}
What did you see instead?
{user: rating:}
The text was updated successfully, but these errors were encountered: