1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-23 18:54:21 +02:00

Merge pull request #128 from carlcarl/fix-json-use-number

Fix #123, `UseNumber` not works with iterator
This commit is contained in:
Tao Wen 2017-07-12 07:04:30 +08:00 committed by GitHub
commit b9dc3ebda7
2 changed files with 35 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package jsoniter package jsoniter
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
) )
@ -273,6 +274,9 @@ func (iter *Iterator) Read() interface{} {
case String: case String:
return iter.ReadString() return iter.ReadString()
case Number: case Number:
if iter.cfg.configBeforeFrozen.UseNumber {
return json.Number(iter.readNumberAsString())
}
return iter.ReadFloat64() return iter.ReadFloat64()
case Nil: case Nil:
iter.skipFourBytes('n', 'u', 'l', 'l') iter.skipFourBytes('n', 'u', 'l', 'l')

View File

@ -2,7 +2,12 @@ package jsoniter
import ( import (
"bytes" "bytes"
"encoding/json"
"fmt"
"strconv"
"testing" "testing"
"github.com/stretchr/testify/require"
) )
func Test_bad_case(t *testing.T) { func Test_bad_case(t *testing.T) {
@ -33,3 +38,29 @@ func Test_bad_case(t *testing.T) {
t.Fatal(count) t.Fatal(count)
} }
} }
func Test_iterator_use_number(t *testing.T) {
// Test UseNumber with iterator Read()
inputs := []string{`2147483647`, `-2147483648`}
for _, input := range inputs {
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(Config{UseNumber: true}.Froze(), input)
expected := json.Number(input)
should.Equal(expected, iter.Read())
})
}
}
func Test_iterator_without_number(t *testing.T) {
inputs := []string{`2147483647`, `-2147483648`}
for _, input := range inputs {
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(ConfigDefault, input)
expected, err := strconv.ParseInt(input, 10, 32)
should.Nil(err)
should.Equal(float64(expected), iter.Read())
})
}
}