Skip to content

json.Unmarshal should use json:key_name conversion #6002

@gopherbot

Description

@gopherbot

by andy@livefyre.com:

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
1. Create JSON byteslice => 
    jsonBlob := []byte(`{"foo_bar": "baz"}`)
2. Define struct => 
    type Quux struct { FooBar string `json:foo_bar`}
3. Unmarshal into declared Quux => 
    var myQuux Quux
    err := json.Unmarshal(jsonBlob, &myQuux)
    fmt.Println(myQuux)

What is the expected output?
{baz}

What do you see instead?
{ }

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
Darwin/OSX

Which version are you using?  (run 'go version')
go version go1.1.1 darwin/amd64

Please provide any additional information below.

I expected the struct field's tag value (described here:
http://golang.org/pkg/encoding/json/#Marshal) to work in both Marshal and Unmarshal.
This would help keep my variable names idiomatic, as well as satisfy external
requirements.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions