Commit 22afb357 authored by Ian Davis's avatar Ian Davis Committed by Daniel Martí

encoding/json: recover saved error context when unmarshalling

Fixes: #27464

Change-Id: I270c56fd0d5ae8787a1293029aff3072f4f52f33
Reviewed-on: https://go-review.googlesource.com/132955Reviewed-by: default avatarDaniel Martí <mvdan@mvdan.cc>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 6b7099ca
...@@ -179,7 +179,7 @@ func (d *decodeState) unmarshal(v interface{}) error { ...@@ -179,7 +179,7 @@ func (d *decodeState) unmarshal(v interface{}) error {
// test must be applied at the top level of the value. // test must be applied at the top level of the value.
err := d.value(rv) err := d.value(rv)
if err != nil { if err != nil {
return err return d.addErrorContext(err)
} }
return d.savedError return d.savedError
} }
......
...@@ -41,6 +41,16 @@ type VOuter struct { ...@@ -41,6 +41,16 @@ type VOuter struct {
V V V V
} }
type W struct {
S SS
}
type SS string
func (*SS) UnmarshalJSON(data []byte) error {
return &UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(SS(""))}
}
// ifaceNumAsFloat64/ifaceNumAsNumber are used to test unmarshaling with and // ifaceNumAsFloat64/ifaceNumAsNumber are used to test unmarshaling with and
// without UseNumber // without UseNumber
var ifaceNumAsFloat64 = map[string]interface{}{ var ifaceNumAsFloat64 = map[string]interface{}{
...@@ -408,6 +418,7 @@ var unmarshalTests = []unmarshalTest{ ...@@ -408,6 +418,7 @@ var unmarshalTests = []unmarshalTest{
{in: `{"X": 23}`, ptr: new(T), out: T{}, err: &UnmarshalTypeError{"number", reflect.TypeOf(""), 8, "T", "X"}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}}, {in: `{"X": 23}`, ptr: new(T), out: T{}, err: &UnmarshalTypeError{"number", reflect.TypeOf(""), 8, "T", "X"}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}},
{in: `{"x": 1}`, ptr: new(tx), out: tx{}}, {in: `{"x": 1}`, ptr: new(tx), out: tx{}},
{in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true}, {in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true},
{in: `{"S": 23}`, ptr: new(W), out: W{}, err: &UnmarshalTypeError{"number", reflect.TypeOf(SS("")), 0, "W", "S"}},
{in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: Number("3")}}, {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: Number("3")}},
{in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: Number("1"), F2: int32(2), F3: Number("3")}, useNumber: true}, {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: Number("1"), F2: int32(2), F3: Number("3")}, useNumber: true},
{in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsFloat64}, {in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsFloat64},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment