Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/decoder/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package json_test
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -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" }`
Expand Down