You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-15 22:50:24 +02:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
71ac16282d | |||
720cd423d0 | |||
9b00d50ac0 | |||
ffc487c633 | |||
10b9ac0971 | |||
bf8b920736 | |||
a7b8414362 | |||
024077e996 | |||
08b5e5796b | |||
c6661824eb | |||
3c55efb1a1 |
@ -8,8 +8,6 @@
|
|||||||
|
|
||||||
A high-performance 100% compatible drop-in replacement of "encoding/json"
|
A high-performance 100% compatible drop-in replacement of "encoding/json"
|
||||||
|
|
||||||
You can also use thrift like JSON using [thrift-iterator](https://github.com/thrift-iterator/go)
|
|
||||||
|
|
||||||
# Benchmark
|
# Benchmark
|
||||||
|
|
||||||

|

|
||||||
|
@ -180,9 +180,9 @@ func readHex(iter *jsoniter.Iterator, b1, b2 byte) byte {
|
|||||||
}
|
}
|
||||||
ret *= 16
|
ret *= 16
|
||||||
if b2 >= '0' && b2 <= '9' {
|
if b2 >= '0' && b2 <= '9' {
|
||||||
ret = b2 - '0'
|
ret += b2 - '0'
|
||||||
} else if b2 >= 'a' && b2 <= 'f' {
|
} else if b2 >= 'a' && b2 <= 'f' {
|
||||||
ret = b2 - 'a' + 10
|
ret += b2 - 'a' + 10
|
||||||
} else {
|
} else {
|
||||||
iter.ReportError("read hex", "expects 0~9 or a~f, but found "+string([]byte{b2}))
|
iter.ReportError("read hex", "expects 0~9 or a~f, but found "+string([]byte{b2}))
|
||||||
return 0
|
return 0
|
||||||
|
@ -22,11 +22,11 @@ func TestBinaryAsStringCodec(t *testing.T) {
|
|||||||
})
|
})
|
||||||
t.Run("non safe set", func(t *testing.T) {
|
t.Run("non safe set", func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
output, err := jsoniter.Marshal([]byte{1, 2, 3, 15})
|
output, err := jsoniter.Marshal([]byte{1, 2, 3, 23})
|
||||||
should.NoError(err)
|
should.NoError(err)
|
||||||
should.Equal(`"\\x01\\x02\\x03\\x0f"`, string(output))
|
should.Equal(`"\\x01\\x02\\x03\\x17"`, string(output))
|
||||||
var val []byte
|
var val []byte
|
||||||
should.NoError(jsoniter.Unmarshal(output, &val))
|
should.NoError(jsoniter.Unmarshal(output, &val))
|
||||||
should.Equal([]byte{1, 2, 3, 15}, val)
|
should.Equal([]byte{1, 2, 3, 23}, val)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
4
go.mod
4
go.mod
@ -6,6 +6,6 @@ require (
|
|||||||
github.com/davecgh/go-spew v1.1.1
|
github.com/davecgh/go-spew v1.1.1
|
||||||
github.com/google/gofuzz v1.0.0
|
github.com/google/gofuzz v1.0.0
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
|
||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
|
github.com/modern-go/reflect2 v1.0.2
|
||||||
github.com/stretchr/testify v1.3.0
|
github.com/stretchr/testify v1.8.0
|
||||||
)
|
)
|
||||||
|
16
go.sum
16
go.sum
@ -5,11 +5,17 @@ github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
|
|||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
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/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
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=
|
||||||
|
@ -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,15 +107,17 @@ func encoderOfMapKey(ctx *ctx, typ reflect2.Type) ValEncoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if typ == textMarshalerType {
|
if typ.Kind() != reflect.String {
|
||||||
return &directTextMarshalerEncoder{
|
if typ == textMarshalerType {
|
||||||
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
|
return &directTextMarshalerEncoder{
|
||||||
|
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if typ.Implements(textMarshalerType) {
|
||||||
if typ.Implements(textMarshalerType) {
|
return &textMarshalerEncoder{
|
||||||
return &textMarshalerEncoder{
|
valType: typ,
|
||||||
valType: typ,
|
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
|
||||||
stringEncoder: ctx.EncoderOf(reflect2.TypeOf("")),
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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