Merge pull request #369 from zeroshade/stream-number-fix

Fix stream tokenizing respecting UseNumber
This commit is contained in:
Masaaki Goshima 2022-05-07 03:59:58 +09:00 committed by GitHub
commit 23bd66f4c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -138,8 +138,11 @@ func (s *Stream) Token() (interface{}, error) {
s.cursor++ s.cursor++
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
bytes := floatBytes(s) bytes := floatBytes(s)
s := *(*string)(unsafe.Pointer(&bytes)) str := *(*string)(unsafe.Pointer(&bytes))
f64, err := strconv.ParseFloat(s, 64) if s.UseNumber {
return json.Number(str), nil
}
f64, err := strconv.ParseFloat(str, 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -7,6 +7,7 @@ package json_test
import ( import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "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 // Test from golang.org/issue/11893
func TestHTTPDecoding(t *testing.T) { func TestHTTPDecoding(t *testing.T) {
const raw = `{ "foo": "bar" }` const raw = `{ "foo": "bar" }`