From 10b9ac0971bab50fe39f3afe993c39306ce27213 Mon Sep 17 00:00:00 2001 From: molon <3739161+molon@users.noreply.github.com> Date: Thu, 15 Sep 2022 21:13:32 +0800 Subject: [PATCH 1/4] std: float64/32 clean up e-09 to e-9 --- stream_float.go | 16 ++++++++++++++++ value_tests/float_test.go | 9 +++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/stream_float.go b/stream_float.go index 826aa59..eddd831 100644 --- a/stream_float.go +++ b/stream_float.go @@ -27,6 +27,14 @@ func (stream *Stream) WriteFloat32(val float32) { } } stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 32) + if fmt == 'e' { + // clean up e-09 to e-9 + n := len(stream.buf) + if n >= 4 && stream.buf[n-4] == 'e' && stream.buf[n-3] == '-' && stream.buf[n-2] == '0' { + stream.buf[n-2] = stream.buf[n-1] + stream.buf = stream.buf[:n-1] + } + } } // WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster @@ -76,6 +84,14 @@ func (stream *Stream) WriteFloat64(val float64) { } } stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 64) + if fmt == 'e' { + // clean up e-09 to e-9 + n := len(stream.buf) + if n >= 4 && stream.buf[n-4] == 'e' && stream.buf[n-3] == '-' && stream.buf[n-2] == '0' { + stream.buf[n-2] = stream.buf[n-1] + stream.buf = stream.buf[:n-1] + } + } } // WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster diff --git a/value_tests/float_test.go b/value_tests/float_test.go index 3c00b26..1f2c007 100644 --- a/value_tests/float_test.go +++ b/value_tests/float_test.go @@ -4,10 +4,11 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/json-iterator/go" - "github.com/stretchr/testify/require" "strconv" "testing" + + jsoniter "github.com/json-iterator/go" + "github.com/stretchr/testify/require" ) func Test_read_float(t *testing.T) { @@ -88,7 +89,7 @@ func Test_write_float32(t *testing.T) { stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0) stream.WriteFloat32(float32(0.0000001)) - should.Equal("1e-07", string(stream.Buffer())) + should.Equal("1e-7", string(stream.Buffer())) } func Test_write_float64(t *testing.T) { @@ -125,5 +126,5 @@ func Test_write_float64(t *testing.T) { stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0) stream.WriteFloat64(float64(0.0000001)) - should.Equal("1e-07", string(stream.Buffer())) + should.Equal("1e-7", string(stream.Buffer())) } From ffc487c633f2c8fbf615be0e3cde9b30889e3082 Mon Sep 17 00:00:00 2001 From: molon <3739161+molon@users.noreply.github.com> Date: Thu, 15 Sep 2022 22:07:27 +0800 Subject: [PATCH 2/4] encoding/json: allow non-string type keys for (un-)marshal https://github.com/golang/go/commit/ffbd31e9f79ad8b6aaeceac1397678e237581064 --- reflect_map.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/reflect_map.go b/reflect_map.go index 5829671..4e479c8 100644 --- a/reflect_map.go +++ b/reflect_map.go @@ -2,11 +2,12 @@ package jsoniter import ( "fmt" - "github.com/modern-go/reflect2" "io" "reflect" "sort" "unsafe" + + "github.com/modern-go/reflect2" ) func decoderOfMap(ctx *ctx, typ reflect2.Type) ValDecoder { @@ -106,15 +107,17 @@ func encoderOfMapKey(ctx *ctx, typ reflect2.Type) ValEncoder { } } - if typ == textMarshalerType { - return &directTextMarshalerEncoder{ - stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")), + if typ.Kind() != reflect.String { + if typ == textMarshalerType { + return &directTextMarshalerEncoder{ + stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")), + } } - } - if typ.Implements(textMarshalerType) { - return &textMarshalerEncoder{ - valType: typ, - stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")), + if typ.Implements(textMarshalerType) { + return &textMarshalerEncoder{ + valType: typ, + stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")), + } } } From 9b00d50ac0321a97f118f6aec473896ba05623e3 Mon Sep 17 00:00:00 2001 From: molon <3739161+molon@users.noreply.github.com> Date: Thu, 15 Sep 2022 22:09:27 +0800 Subject: [PATCH 3/4] Revert "std: float64/32 clean up e-09 to e-9" This reverts commit 10b9ac0971bab50fe39f3afe993c39306ce27213. --- stream_float.go | 16 ---------------- value_tests/float_test.go | 9 ++++----- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/stream_float.go b/stream_float.go index eddd831..826aa59 100644 --- a/stream_float.go +++ b/stream_float.go @@ -27,14 +27,6 @@ func (stream *Stream) WriteFloat32(val float32) { } } stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 32) - if fmt == 'e' { - // clean up e-09 to e-9 - n := len(stream.buf) - if n >= 4 && stream.buf[n-4] == 'e' && stream.buf[n-3] == '-' && stream.buf[n-2] == '0' { - stream.buf[n-2] = stream.buf[n-1] - stream.buf = stream.buf[:n-1] - } - } } // WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster @@ -84,14 +76,6 @@ func (stream *Stream) WriteFloat64(val float64) { } } stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 64) - if fmt == 'e' { - // clean up e-09 to e-9 - n := len(stream.buf) - if n >= 4 && stream.buf[n-4] == 'e' && stream.buf[n-3] == '-' && stream.buf[n-2] == '0' { - stream.buf[n-2] = stream.buf[n-1] - stream.buf = stream.buf[:n-1] - } - } } // WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster diff --git a/value_tests/float_test.go b/value_tests/float_test.go index 1f2c007..3c00b26 100644 --- a/value_tests/float_test.go +++ b/value_tests/float_test.go @@ -4,11 +4,10 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/json-iterator/go" + "github.com/stretchr/testify/require" "strconv" "testing" - - jsoniter "github.com/json-iterator/go" - "github.com/stretchr/testify/require" ) func Test_read_float(t *testing.T) { @@ -89,7 +88,7 @@ func Test_write_float32(t *testing.T) { stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0) stream.WriteFloat32(float32(0.0000001)) - should.Equal("1e-7", string(stream.Buffer())) + should.Equal("1e-07", string(stream.Buffer())) } func Test_write_float64(t *testing.T) { @@ -126,5 +125,5 @@ func Test_write_float64(t *testing.T) { stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0) stream.WriteFloat64(float64(0.0000001)) - should.Equal("1e-7", string(stream.Buffer())) + should.Equal("1e-07", string(stream.Buffer())) } From 720cd423d01f451f4d2e0e76829840ddd5696d14 Mon Sep 17 00:00:00 2001 From: molon <3739161+molon@users.noreply.github.com> Date: Thu, 15 Sep 2022 22:10:44 +0800 Subject: [PATCH 4/4] encoding/json: use standard ES6 formatting for numbers during marshal https://github.com/golang/go/commit/92b3e3651dc44f54b458f171f641779f10fbaec0 --- stream_float.go | 16 ++++++++++++++++ value_tests/float_test.go | 9 +++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/stream_float.go b/stream_float.go index 826aa59..eddd831 100644 --- a/stream_float.go +++ b/stream_float.go @@ -27,6 +27,14 @@ func (stream *Stream) WriteFloat32(val float32) { } } stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 32) + if fmt == 'e' { + // clean up e-09 to e-9 + n := len(stream.buf) + if n >= 4 && stream.buf[n-4] == 'e' && stream.buf[n-3] == '-' && stream.buf[n-2] == '0' { + stream.buf[n-2] = stream.buf[n-1] + stream.buf = stream.buf[:n-1] + } + } } // WriteFloat32Lossy write float32 to stream with ONLY 6 digits precision although much much faster @@ -76,6 +84,14 @@ func (stream *Stream) WriteFloat64(val float64) { } } stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 64) + if fmt == 'e' { + // clean up e-09 to e-9 + n := len(stream.buf) + if n >= 4 && stream.buf[n-4] == 'e' && stream.buf[n-3] == '-' && stream.buf[n-2] == '0' { + stream.buf[n-2] = stream.buf[n-1] + stream.buf = stream.buf[:n-1] + } + } } // WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster diff --git a/value_tests/float_test.go b/value_tests/float_test.go index 3c00b26..1f2c007 100644 --- a/value_tests/float_test.go +++ b/value_tests/float_test.go @@ -4,10 +4,11 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/json-iterator/go" - "github.com/stretchr/testify/require" "strconv" "testing" + + jsoniter "github.com/json-iterator/go" + "github.com/stretchr/testify/require" ) func Test_read_float(t *testing.T) { @@ -88,7 +89,7 @@ func Test_write_float32(t *testing.T) { stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0) stream.WriteFloat32(float32(0.0000001)) - should.Equal("1e-07", string(stream.Buffer())) + should.Equal("1e-7", string(stream.Buffer())) } func Test_write_float64(t *testing.T) { @@ -125,5 +126,5 @@ func Test_write_float64(t *testing.T) { stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0) stream.WriteFloat64(float64(0.0000001)) - should.Equal("1e-07", string(stream.Buffer())) + should.Equal("1e-7", string(stream.Buffer())) }