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: decoding can match both field name and struct tag name #3566

Closed
dsymonds opened this issue Apr 25, 2012 · 3 comments

Comments

Projects
None yet
3 participants
@dsymonds
Copy link
Member

commented Apr 25, 2012

I was getting terribly confused today trying to figure this out:
----------
package main

import (
    "encoding/json"
    "fmt"
)

var s =
`{"owner_email":"first","owner":"second"}`

func main() {
    var data struct {
        Owner string `json:"owner_email"`
    }
    if err := json.Unmarshal([]byte(s), &data); err != nil {
        fmt.Printf("Malformed JSON: %v", err)
        return
    }

    fmt.Printf("data: %+v", data)
}
----------

I only cared about the "owner_email" field, so was just trying to select it.
Unfortunately, the decoding of the later "owner" field overwrites it. I'm not
sure it's a bug, but I'm leaning that way. My reason for thinking it's a bug is that the
`json:"owner_email"` is declaring that the JSON name for that field is
"owner_email", and thus the "owner" field should *not* be matching
against it, even if it was the only piece of the input.

This is easy enough to work around (I just have to rename the Owner field); I'm not sure
whether this qualifies as a clear enough bug to warrant a technical change to the API.
@rsc

This comment has been minimized.

Copy link
Contributor

commented Apr 25, 2012

Comment 1:

sounds like a bug to me
@dsymonds

This comment has been minimized.

Copy link
Member Author

commented Apr 29, 2012

Comment 2:

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

Owner changed to @dsymonds.

Status changed to Started.

@dsymonds

This comment has been minimized.

Copy link
Member Author

commented May 1, 2012

Comment 3:

This issue was closed by revision c3c8e35.

Status changed to Fixed.

@dsymonds dsymonds added fixed labels May 1, 2012

@dsymonds dsymonds self-assigned this May 1, 2012

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