1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

7 Commits

Author SHA1 Message Date
71ac16282d Merge pull request #635 from molon/fix-tests
encoding/json: Compatibility optimization
2022-09-16 07:37:16 +08:00
720cd423d0 encoding/json: use standard ES6 formatting for numbers during marshal
92b3e3651d
2022-09-15 22:10:44 +08:00
9b00d50ac0 Revert "std: float64/32 clean up e-09 to e-9"
This reverts commit 10b9ac0971.
2022-09-15 22:09:27 +08:00
ffc487c633 encoding/json: allow non-string type keys for (un-)marshal
ffbd31e9f7
2022-09-15 22:07:27 +08:00
10b9ac0971 std: float64/32 clean up e-09 to e-9 2022-09-15 21:13:32 +08:00
bf8b920736 Merge pull request #628 from HassanBaker/upgrade-stretchr
bumped stretchr to v1.8.0 to get rid of yaml v2 vuln
2022-08-05 15:01:11 +08:00
a7b8414362 bumped stretchr to v1.8.0 to get rid of yaml v2 vuln
Signed-off-by: Hassan Baker <me@hassanbaker.dev>
2022-08-04 17:25:33 +01:00
5 changed files with 43 additions and 16 deletions

2
go.mod
View File

@ -7,5 +7,5 @@ require (
github.com/google/gofuzz v1.0.0
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
github.com/modern-go/reflect2 v1.0.2
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.8.0
)

11
go.sum
View File

@ -10,5 +10,12 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -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("")),
}
}
}

View File

@ -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

View File

@ -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()))
}