Skip to content

encoding/json: UnmarshalTypeError.Error message seems insensible #43126

@dsnet

Description

@dsnet

Consider this snippet:

type GoStruct1 struct {
	AlphaField GoStruct2 `json:"alpha_field"`
}
type GoStruct2 struct {
	BravoField []GoStruct3 `json:"bravo_field"`
}
type GoStruct3 struct {
	StringField string `json:"string_field"`
}

func main() {
	fmt.Println(json.Unmarshal([]byte(`{"alpha_field":{"bravo_field":[{"string_field": 3.14159}]}}`), new(GoStruct1)))
}

On Go1.15, this prints:

json: cannot unmarshal number into Go struct field GoStruct3.alpha_field.bravo_field.string_field of type string

Notice that it combines two different namespaces where it is:

{{GoTypeName}}.{{JSONPath}}

and that the message says that it is a "Go struct field" when it isn't.
Also, it uses GoStruct3, when it makes little sense.
At minimum, it should at least used GoStruct1.

\cc @mvdan

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions