From 865b21589003a0b3921986f029f6d9e4f16e8d52 Mon Sep 17 00:00:00 2001 From: Matthew Topol Date: Thu, 5 May 2022 12:12:27 -0400 Subject: [PATCH] fix stream tokenizing respecting UseNumber --- internal/decoder/stream.go | 7 +++++-- stream_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/decoder/stream.go b/internal/decoder/stream.go index 332d47d..6f337d7 100644 --- a/internal/decoder/stream.go +++ b/internal/decoder/stream.go @@ -138,8 +138,11 @@ func (s *Stream) Token() (interface{}, error) { s.cursor++ case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': bytes := floatBytes(s) - s := *(*string)(unsafe.Pointer(&bytes)) - f64, err := strconv.ParseFloat(s, 64) + str := *(*string)(unsafe.Pointer(&bytes)) + if s.UseNumber { + return json.Number(str), nil + } + f64, err := strconv.ParseFloat(str, 64) if err != nil { return nil, err } diff --git a/stream_test.go b/stream_test.go index a834e1f..edb680c 100644 --- a/stream_test.go +++ b/stream_test.go @@ -7,6 +7,7 @@ package json_test import ( "bytes" "compress/gzip" + "fmt" "io" "io/ioutil" "log" @@ -431,6 +432,16 @@ func TestDecodeInStream(t *testing.T) { } } +func TestDecodeStreamUseNumber(t *testing.T) { + dec := json.NewDecoder(strings.NewReader(`3.14`)) + dec.UseNumber() + v, err := dec.Token() + if err != nil { + t.Errorf("unexpected error: %#v", err) + } + assertEq(t, "json.Number", "json.Number", fmt.Sprintf("%T", v)) +} + // Test from golang.org/issue/11893 func TestHTTPDecoding(t *testing.T) { const raw = `{ "foo": "bar" }`