]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: recover saved error context when unmarshalling
authorIan Davis <nospam@iandavis.com>
Mon, 3 Sep 2018 10:20:23 +0000 (11:20 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Thu, 6 Sep 2018 08:38:44 +0000 (08:38 +0000)
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>

src/encoding/json/decode.go
src/encoding/json/decode_test.go

index fd2bf92dc24a1625b7ffd81a251ba904eba3093b..82dc78083a7ca9f62f33efb78889401828c27b98 100644 (file)
@@ -179,7 +179,7 @@ func (d *decodeState) unmarshal(v interface{}) error {
        // test must be applied at the top level of the value.
        err := d.value(rv)
        if err != nil {
-               return err
+               return d.addErrorContext(err)
        }
        return d.savedError
 }
index b84bbabfcd801e572e3939a434d1cdeda2805cab..defa97e40fdd080ebbb00f52e076317c64dcd170 100644 (file)
@@ -41,6 +41,16 @@ type VOuter struct {
        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
 // without UseNumber
 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": 1}`, ptr: new(tx), out: tx{}},
        {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: 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},