You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-15 22:50:24 +02:00
Merge pull request #635 from molon/fix-tests
encoding/json: Compatibility optimization
This commit is contained in:
@ -2,11 +2,12 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/modern-go/reflect2"
|
|
||||||
"io"
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/modern-go/reflect2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func decoderOfMap(ctx *ctx, typ reflect2.Type) ValDecoder {
|
func decoderOfMap(ctx *ctx, typ reflect2.Type) ValDecoder {
|
||||||
@ -106,6 +107,7 @@ func encoderOfMapKey(ctx *ctx, typ reflect2.Type) ValEncoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if typ.Kind() != reflect.String {
|
||||||
if typ == textMarshalerType {
|
if typ == textMarshalerType {
|
||||||
return &directTextMarshalerEncoder{
|
return &directTextMarshalerEncoder{
|
||||||
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
|
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
|
||||||
@ -117,6 +119,7 @@ func encoderOfMapKey(ctx *ctx, typ reflect2.Type) ValEncoder {
|
|||||||
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
|
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch typ.Kind() {
|
switch typ.Kind() {
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
|
@ -27,6 +27,14 @@ func (stream *Stream) WriteFloat32(val float32) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
stream.buf = strconv.AppendFloat(stream.buf, float64(val), fmt, -1, 32)
|
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
|
// 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)
|
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
|
// WriteFloat64Lossy write float64 to stream with ONLY 6 digits precision although much much faster
|
||||||
|
@ -4,10 +4,11 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/json-iterator/go"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_float(t *testing.T) {
|
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 = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
|
||||||
stream.WriteFloat32(float32(0.0000001))
|
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) {
|
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 = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 0)
|
||||||
stream.WriteFloat64(float64(0.0000001))
|
stream.WriteFloat64(float64(0.0000001))
|
||||||
should.Equal("1e-07", string(stream.Buffer()))
|
should.Equal("1e-7", string(stream.Buffer()))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user