diff --git a/src/encoding/json/bench_test.go b/src/encoding/json/bench_test.go index f7bcf8073c0..032114cac14 100644 --- a/src/encoding/json/bench_test.go +++ b/src/encoding/json/bench_test.go @@ -571,3 +571,14 @@ func BenchmarkNumberIsValidRegexp(b *testing.B) { jsonNumberRegexp.MatchString(s) } } + +func BenchmarkUnmarshalNumber(b *testing.B) { + b.ReportAllocs() + data := []byte(`"-61657.61667E+61673"`) + var number Number + for i := 0; i < b.N; i++ { + if err := Unmarshal(data, &number); err != nil { + b.Fatal("Unmarshal:", err) + } + } +} diff --git a/src/encoding/json/decode.go b/src/encoding/json/decode.go index e1bc9753b11..ce566f79556 100644 --- a/src/encoding/json/decode.go +++ b/src/encoding/json/decode.go @@ -943,10 +943,11 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool } v.SetBytes(b[:n]) case reflect.String: - if v.Type() == numberType && !isValidNumber(string(s)) { + t := string(s) + if v.Type() == numberType && !isValidNumber(t) { return fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", item) } - v.SetString(string(s)) + v.SetString(t) case reflect.Interface: if v.NumMethod() == 0 { v.Set(reflect.ValueOf(string(s)))