Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 71ac162

Browse files
authored
Merge pull request #635 from molon/fix-tests
encoding/json: Compatibility optimization
2 parents bf8b920 + 720cd42 commit 71ac162

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

‎reflect_map.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package jsoniter
22

33
import (
44
"fmt"
5-
"github.com/modern-go/reflect2"
65
"io"
76
"reflect"
87
"sort"
98
"unsafe"
9+
10+
"github.com/modern-go/reflect2"
1011
)
1112

1213
func decoderOfMap(ctx *ctx, typ reflect2.Type) ValDecoder {
@@ -106,15 +107,17 @@ func encoderOfMapKey(ctx *ctx, typ reflect2.Type) ValEncoder {
106107
}
107108
}
108109

109-
if typ == textMarshalerType {
110-
return &directTextMarshalerEncoder{
111-
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
110+
if typ.Kind() != reflect.String {
111+
if typ == textMarshalerType {
112+
return &directTextMarshalerEncoder{
113+
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
114+
}
112115
}
113-
}
114-
iftyp.Implements(textMarshalerType) {
115-
return&textMarshalerEncoder{
116-
valType: typ,
117-
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
116+
iftyp.Implements(textMarshalerType) {
117+
return&textMarshalerEncoder{
118+
valType: typ,
119+
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
120+
}
118121
}
119122
}
120123

‎stream_float.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ func (stream *Stream) WriteFloat32(val float32) {
2727
}
2828
}
2929
stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 32)
30+
if fmt == 'e' {
31+
// clean up e-09 to e-9
32+
n := len(stream.buf)
33+
if n >= 4 && stream.buf[n-4] == 'e' && stream.buf[n-3] == '-' && stream.buf[n-2] == '0' {
34+
stream.buf[n-2] = stream.buf[n-1]
35+
stream.buf = stream.buf[:n-1]
36+
}
37+
}
3038
}
3139

3240
// WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster
@@ -76,6 +84,14 @@ func (stream *Stream) WriteFloat64(val float64) {
7684
}
7785
}
7886
stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 64)
87+
if fmt == 'e' {
88+
// clean up e-09 to e-9
89+
n := len(stream.buf)
90+
if n >= 4 && stream.buf[n-4] == 'e' && stream.buf[n-3] == '-' && stream.buf[n-2] == '0' {
91+
stream.buf[n-2] = stream.buf[n-1]
92+
stream.buf = stream.buf[:n-1]
93+
}
94+
}
7995
}
8096

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

‎value_tests/float_test.go

Lines changed: 5 additions & 4 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) {
@@ -88,7 +89,7 @@ func Test_write_float32(t *testing.T) {
8889

8990
stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
9091
stream.WriteFloat32(float32(0.0000001))
91-
should.Equal("1e-07", string(stream.Buffer()))
92+
should.Equal("1e-7", string(stream.Buffer()))
9293
}
9394

9495
func Test_write_float64(t *testing.T) {
@@ -125,5 +126,5 @@ func Test_write_float64(t *testing.T) {
125126

126127
stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
127128
stream.WriteFloat64(float64(0.0000001))
128-
should.Equal("1e-07", string(stream.Buffer()))
129+
should.Equal("1e-7", string(stream.Buffer()))
129130
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /