You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unmarshal (but not Decode) panics instead of returning an error when a type's
UnmarshalJSON returns an error.
For example, add this to decode_test.go:
// Time3339 is a time.Time which encodes to and from JSON
// as an RFC 3339 time in UTC.
type Time3339 time.Time
func (t *Time3339) UnmarshalJSON(b []byte) error {
if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
return fmt.Errorf("types: failed to unmarshal non-string value %q as an RFC 3339 time")
}
tm, err := time.Parse(time.RFC3339, string(b[1:len(b)-1]))
if err != nil {
return err
}
*t = Time3339(tm)
return nil
}
func TestUnmarshalJSONLiteralError(t *testing.T) {
var t3 Time3339
err := Unmarshal([]byte(`"0000-00-00T00:00:00Z"`), &t3)
if err == nil {
t.Fatalf("expected error; got time %v", time.Time(t3))
}
if !strings.Contains(err.Error(), "range") {
t.Errorf("got err = %v; want out of range error", err)
}
}
It then panics with:
--- PASS: TestUnmarshalJSONLiteralError (0.00 seconds)
panic: parsing time "0000-00-00T00:00:00Z": month out of range [recovered]
panic: parsing time "0000-00-00T00:00:00Z": month out of range
goroutine 22 [running]:
testing.func·004(0x10384fa8, 0x10384100)
/Users/bradfitz/go/src/pkg/testing/testing.go:309 +0xb3
encoding/json.(*decodeState).error(0xc2000ff800, 0xc200383c30, 0xc200181460, 0x16)
/Users/bradfitz/go/src/pkg/encoding/json/decode.go:203 +0x47
encoding/json.(*decodeState).literalStore(0xc2000ff800, 0xc2000fe660, 0x16, 0x16,
0x18ea08, ...)
/Users/bradfitz/go/src/pkg/encoding/json/decode.go:621 +0x286
encoding/json.Unmarshal(0xc2000fe660, 0x16, 0x16, 0x177d98, 0xc2000fe640, ...)
/Users/bradfitz/go/src/pkg/encoding/json/decode.go:82 +0x266
encoding/json.TestUnmarshalJSONLiteralError(0xc2002d0750, 0x1dd35e08)
/Users/bradfitz/go/src/pkg/encoding/json/decode_test.go:1136 +0xbd
testing.tRunner(0xc2002d0750, 0x2b91a8, 0x0, 0x0)
/Users/bradfitz/go/src/pkg/testing/testing.go:314 +0x6c
created by testing.RunTests
/Users/bradfitz/go/src/pkg/testing/testing.go:429 +0x875
goroutine 1 [chan receive]:
testing.RunTests(0x2000, 0x2b8fe0, 0x29, 0x29, 0x1, ...)
/Users/bradfitz/go/src/pkg/testing/testing.go:430 +0x898
testing.Main(0x2000, 0x2b8fe0, 0x29, 0x29, 0x2b6350, ...)
/Users/bradfitz/go/src/pkg/testing/testing.go:326 +0x8a
main.main()
encoding/json/_test/_testmain.go:149 +0x9a
exit status 2
FAIL encoding/json 0.101s
The text was updated successfully, but these errors were encountered:
Caused by the optimization introduced in:
changeset: 15532:de89928ea00f
user: Rick Arnold <rickarnoldjr@gmail.com>
date: Wed Jan 30 17:53:48 2013 -0800
summary: encoding/json: improve performance of Unmarshal on primitive types
That's the second attempt and the second time it broke Unmarshal.
We should roll it back and stop trying.
The text was updated successfully, but these errors were encountered: