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: Given struct tags Unmarshal should be strict #14737

Closed
rolandshoemaker opened this issue Mar 9, 2016 · 1 comment

Comments

Projects
None yet
3 participants
@rolandshoemaker
Copy link

commented Mar 9, 2016

  1. What version of Go are you using (go version)?
    go1.6
  2. What operating system and processor architecture are you using (go env)?
    Ubuntu 15.10 amd64
  3. What did you do?
package main

import "fmt"
import "encoding/json"

type t struct {
    Test string `json:"test"`
}

func main() {
    var test t
    err := json.Unmarshal([]byte(`{"test":"good","TEST":"bad"}`), &test)
    fmt.Println(test, err)
}
  1. What did you expect to see?

When a struct tag is provided the Unmarshal function shouldn't attempt to do a 'best match' against the field name and should instead only accept keys that exactly match the tag. In this case there is a leading valid field but in the case where there isn't one nothing should be parsed out.

# go run test.go
# {good} <nil>
  1. What did you see instead?
# go run test.go
# {bad} <nil>
@minux

This comment has been minimized.

Copy link
Member

commented Mar 9, 2016

This is the documented behavior, and we can't change it
without breaking the Go 1 compatibility promise.

"To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match."

https://golang.org/pkg/encoding/json/#Unmarshal

@minux minux closed this Mar 9, 2016

@golang golang locked and limited conversation to collaborators Mar 13, 2017

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.