mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json: recover saved error context when unmarshalling
Fixes: #27464 Change-Id: I270c56fd0d5ae8787a1293029aff3072f4f52f33 Reviewed-on: https://go-review.googlesource.com/132955 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
6b7099caa1
commit
22afb3571c
2 changed files with 12 additions and 1 deletions
|
|
@ -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 {
|
||||||
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{
|
||||||
{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},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue