Skip to content

Commit ef53dc6

Browse files
committed
Cleanup zero for float encoder and keep compatility with std.
1 parent bf8b920 commit ef53dc6

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

stream_float.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ func init() {
1212
pow10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
1313
}
1414

15+
func (stream *Stream) cleanupZero(b []byte, fmt byte) []byte {
16+
if fmt == 'e' {
17+
// clean up e-09 to e-9
18+
n := len(b)
19+
if n >= 4 && b[n-4] == 'e' && b[n-3] == '-' && b[n-2] == '0' {
20+
b[n-2] = b[n-1]
21+
b = b[:n-1]
22+
}
23+
}
24+
return b
25+
}
26+
1527
// WriteFloat32 write float32 to stream
1628
func (stream *Stream) WriteFloat32(val float32) {
1729
if math.IsInf(float64(val), 0) || math.IsNaN(float64(val)) {
@@ -27,6 +39,7 @@ func (stream *Stream) WriteFloat32(val float32) {
2739
}
2840
}
2941
stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 32)
42+
stream.buf = stream.cleanupZero(stream.buf, fmt)
3043
}
3144

3245
// WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster
@@ -76,6 +89,7 @@ func (stream *Stream) WriteFloat64(val float64) {
7689
}
7790
}
7891
stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 64)
92+
stream.buf = stream.cleanupZero(stream.buf, fmt)
7993
}
8094

8195
// WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster

value_tests/float_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"github.com/json-iterator/go"
8-
"github.com/stretchr/testify/require"
97
"strconv"
108
"testing"
9+
10+
jsoniter "github.com/json-iterator/go"
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
func Test_read_float(t *testing.T) {
@@ -86,9 +87,13 @@ func Test_write_float32(t *testing.T) {
8687
should.Nil(stream.Error)
8788
should.Equal("abcdefg1.123456", buf.String())
8889

90+
val := float32(0.0000001)
8991
stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
90-
stream.WriteFloat32(float32(0.0000001))
91-
should.Equal("1e-07", string(stream.Buffer()))
92+
stream.WriteFloat32(val)
93+
output, err := json.Marshal(val)
94+
should.Nil(err)
95+
should.Equal("1e-7", string(stream.Buffer()))
96+
should.Equal(string(output), string(stream.Buffer()))
9297
}
9398

9499
func Test_write_float64(t *testing.T) {
@@ -123,7 +128,11 @@ func Test_write_float64(t *testing.T) {
123128
should.Nil(stream.Error)
124129
should.Equal("abcdefg1.123456", buf.String())
125130

131+
val := float64(0.0000001)
126132
stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
127-
stream.WriteFloat64(float64(0.0000001))
128-
should.Equal("1e-07", string(stream.Buffer()))
133+
stream.WriteFloat64(val)
134+
output, err := json.Marshal(val)
135+
should.Nil(err)
136+
should.Equal("1e-7", string(stream.Buffer()))
137+
should.Equal(string(output), string(stream.Buffer()))
129138
}

0 commit comments

Comments
 (0)