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
9 Commits
revert-55-
...
jsoniter-g
Author | SHA1 | Date | |
---|---|---|---|
69bc64b6d8 | |||
e0e2423e9a | |||
a6ea770365 | |||
5f22e50c89 | |||
d867c8ba5c | |||
d0418857ce | |||
48e9f6ec84 | |||
acddcf5bbf | |||
788918b85d |
@ -14,9 +14,7 @@ package jsoniter
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"reflect"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,41 +23,12 @@ import (
|
|||||||
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
|
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
|
||||||
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
|
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
|
||||||
func Unmarshal(data []byte, v interface{}) error {
|
func Unmarshal(data []byte, v interface{}) error {
|
||||||
data = data[:lastNotSpacePos(data)]
|
return ConfigOfDefault.Unmarshal(data, v)
|
||||||
iter := ParseBytes(data)
|
|
||||||
typ := reflect.TypeOf(v)
|
|
||||||
if typ.Kind() != reflect.Ptr {
|
|
||||||
// return non-pointer error
|
|
||||||
return errors.New("the second param must be ptr type")
|
|
||||||
}
|
|
||||||
iter.ReadVal(v)
|
|
||||||
if iter.head == iter.tail {
|
|
||||||
iter.loadMore()
|
|
||||||
}
|
|
||||||
if iter.Error == io.EOF {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if iter.Error == nil {
|
|
||||||
iter.reportError("Unmarshal", "there are bytes left after unmarshal")
|
|
||||||
}
|
|
||||||
return iter.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalAny adapts to
|
// UnmarshalAny adapts to
|
||||||
func UnmarshalAny(data []byte) (Any, error) {
|
func UnmarshalAny(data []byte) (Any, error) {
|
||||||
data = data[:lastNotSpacePos(data)]
|
return ConfigOfDefault.UnmarshalAny(data)
|
||||||
iter := ParseBytes(data)
|
|
||||||
any := iter.ReadAny()
|
|
||||||
if iter.head == iter.tail {
|
|
||||||
iter.loadMore()
|
|
||||||
}
|
|
||||||
if iter.Error == io.EOF {
|
|
||||||
return any, nil
|
|
||||||
}
|
|
||||||
if iter.Error == nil {
|
|
||||||
iter.reportError("UnmarshalAny", "there are bytes left after unmarshal")
|
|
||||||
}
|
|
||||||
return any, iter.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func lastNotSpacePos(data []byte) int {
|
func lastNotSpacePos(data []byte) int {
|
||||||
@ -72,37 +41,11 @@ func lastNotSpacePos(data []byte) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UnmarshalFromString(str string, v interface{}) error {
|
func UnmarshalFromString(str string, v interface{}) error {
|
||||||
data := []byte(str)
|
return ConfigOfDefault.UnmarshalFromString(str, v)
|
||||||
data = data[:lastNotSpacePos(data)]
|
|
||||||
iter := ParseBytes(data)
|
|
||||||
iter.ReadVal(v)
|
|
||||||
if iter.head == iter.tail {
|
|
||||||
iter.loadMore()
|
|
||||||
}
|
|
||||||
if iter.Error == io.EOF {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if iter.Error == nil {
|
|
||||||
iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
|
|
||||||
}
|
|
||||||
return iter.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnmarshalAnyFromString(str string) (Any, error) {
|
func UnmarshalAnyFromString(str string) (Any, error) {
|
||||||
data := []byte(str)
|
return ConfigOfDefault.UnmarshalAnyFromString(str)
|
||||||
data = data[:lastNotSpacePos(data)]
|
|
||||||
iter := ParseBytes(data)
|
|
||||||
any := iter.ReadAny()
|
|
||||||
if iter.head == iter.tail {
|
|
||||||
iter.loadMore()
|
|
||||||
}
|
|
||||||
if iter.Error == io.EOF {
|
|
||||||
return any, nil
|
|
||||||
}
|
|
||||||
if iter.Error == nil {
|
|
||||||
iter.reportError("UnmarshalAnyFromString", "there are bytes left after unmarshal")
|
|
||||||
}
|
|
||||||
return nil, iter.Error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal adapts to json/encoding Marshal API
|
// Marshal adapts to json/encoding Marshal API
|
||||||
@ -110,20 +53,11 @@ func UnmarshalAnyFromString(str string) (Any, error) {
|
|||||||
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
|
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
|
||||||
// Refer to https://godoc.org/encoding/json#Marshal for more information
|
// Refer to https://godoc.org/encoding/json#Marshal for more information
|
||||||
func Marshal(v interface{}) ([]byte, error) {
|
func Marshal(v interface{}) ([]byte, error) {
|
||||||
stream := NewStream(nil, 256)
|
return ConfigOfDefault.Marshal(v)
|
||||||
stream.WriteVal(v)
|
|
||||||
if stream.Error != nil {
|
|
||||||
return nil, stream.Error
|
|
||||||
}
|
|
||||||
return stream.Buffer(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func MarshalToString(v interface{}) (string, error) {
|
func MarshalToString(v interface{}) (string, error) {
|
||||||
buf, err := Marshal(v)
|
return ConfigOfDefault.MarshalToString(v)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(buf), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDecoder adapts to json/stream NewDecoder API.
|
// NewDecoder adapts to json/stream NewDecoder API.
|
||||||
@ -133,8 +67,7 @@ func MarshalToString(v interface{}) (string, error) {
|
|||||||
// Instead of a json/encoding Decoder, an AdaptedDecoder is returned
|
// Instead of a json/encoding Decoder, an AdaptedDecoder is returned
|
||||||
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
|
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
|
||||||
func NewDecoder(reader io.Reader) *AdaptedDecoder {
|
func NewDecoder(reader io.Reader) *AdaptedDecoder {
|
||||||
iter := Parse(reader, 512)
|
return ConfigOfDefault.NewDecoder(reader)
|
||||||
return &AdaptedDecoder{iter}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AdaptedDecoder reads and decodes JSON values from an input stream.
|
// AdaptedDecoder reads and decodes JSON values from an input stream.
|
||||||
@ -172,8 +105,7 @@ func (decoder *AdaptedDecoder) UseNumber() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewEncoder(writer io.Writer) *AdaptedEncoder {
|
func NewEncoder(writer io.Writer) *AdaptedEncoder {
|
||||||
stream := NewStream(writer, 512)
|
return ConfigOfDefault.NewEncoder(writer)
|
||||||
return &AdaptedEncoder{stream}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AdaptedEncoder struct {
|
type AdaptedEncoder struct {
|
||||||
@ -187,5 +119,11 @@ func (adapter *AdaptedEncoder) Encode(val interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
|
func (adapter *AdaptedEncoder) SetIndent(prefix, indent string) {
|
||||||
adapter.stream.IndentionStep = len(indent)
|
adapter.stream.cfg.indentionStep = len(indent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (adapter *AdaptedEncoder) SetEscapeHTML(escapeHtml bool) {
|
||||||
|
config := adapter.stream.cfg.configBeforeFrozen
|
||||||
|
config.EscapeHtml = escapeHtml
|
||||||
|
adapter.stream.cfg = config.Froze()
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ func (any *arrayLazyAny) ValueType() ValueType {
|
|||||||
func (any *arrayLazyAny) Parse() *Iterator {
|
func (any *arrayLazyAny) Parse() *Iterator {
|
||||||
iter := any.iter
|
iter := any.iter
|
||||||
if iter == nil {
|
if iter == nil {
|
||||||
iter = NewIterator()
|
iter = NewIterator(ConfigOfDefault)
|
||||||
any.iter = iter
|
any.iter = iter
|
||||||
}
|
}
|
||||||
iter.ResetBytes(any.remaining)
|
iter.ResetBytes(any.remaining)
|
||||||
@ -287,7 +287,7 @@ func (any *arrayLazyAny) IterateArray() (func() (Any, bool), bool) {
|
|||||||
// read from buffer
|
// read from buffer
|
||||||
iter := any.iter
|
iter := any.iter
|
||||||
if iter == nil {
|
if iter == nil {
|
||||||
iter = NewIterator()
|
iter = NewIterator(ConfigOfDefault)
|
||||||
any.iter = iter
|
any.iter = iter
|
||||||
}
|
}
|
||||||
iter.ResetBytes(remaining)
|
iter.ResetBytes(remaining)
|
||||||
|
@ -17,7 +17,7 @@ type float64LazyAny struct {
|
|||||||
func (any *float64LazyAny) Parse() *Iterator {
|
func (any *float64LazyAny) Parse() *Iterator {
|
||||||
iter := any.iter
|
iter := any.iter
|
||||||
if iter == nil {
|
if iter == nil {
|
||||||
iter = NewIterator()
|
iter = NewIterator(ConfigOfDefault)
|
||||||
}
|
}
|
||||||
iter.ResetBytes(any.buf)
|
iter.ResetBytes(any.buf)
|
||||||
return iter
|
return iter
|
||||||
|
@ -21,7 +21,7 @@ func (any *int64LazyAny) ValueType() ValueType {
|
|||||||
func (any *int64LazyAny) Parse() *Iterator {
|
func (any *int64LazyAny) Parse() *Iterator {
|
||||||
iter := any.iter
|
iter := any.iter
|
||||||
if iter == nil {
|
if iter == nil {
|
||||||
iter = NewIterator()
|
iter = NewIterator(ConfigOfDefault)
|
||||||
}
|
}
|
||||||
iter.ResetBytes(any.buf)
|
iter.ResetBytes(any.buf)
|
||||||
return iter
|
return iter
|
||||||
|
@ -22,7 +22,7 @@ func (any *objectLazyAny) ValueType() ValueType {
|
|||||||
func (any *objectLazyAny) Parse() *Iterator {
|
func (any *objectLazyAny) Parse() *Iterator {
|
||||||
iter := any.iter
|
iter := any.iter
|
||||||
if iter == nil {
|
if iter == nil {
|
||||||
iter = NewIterator()
|
iter = NewIterator(ConfigOfDefault)
|
||||||
any.iter = iter
|
any.iter = iter
|
||||||
}
|
}
|
||||||
iter.ResetBytes(any.remaining)
|
iter.ResetBytes(any.remaining)
|
||||||
@ -308,7 +308,7 @@ func (any *objectLazyAny) IterateObject() (func() (string, Any, bool), bool) {
|
|||||||
// read from buffer
|
// read from buffer
|
||||||
iter := any.iter
|
iter := any.iter
|
||||||
if iter == nil {
|
if iter == nil {
|
||||||
iter = NewIterator()
|
iter = NewIterator(ConfigOfDefault)
|
||||||
any.iter = iter
|
any.iter = iter
|
||||||
}
|
}
|
||||||
iter.ResetBytes(remaining)
|
iter.ResetBytes(remaining)
|
||||||
|
@ -20,7 +20,7 @@ func (any *stringLazyAny) ValueType() ValueType {
|
|||||||
func (any *stringLazyAny) Parse() *Iterator {
|
func (any *stringLazyAny) Parse() *Iterator {
|
||||||
iter := any.iter
|
iter := any.iter
|
||||||
if iter == nil {
|
if iter == nil {
|
||||||
iter = NewIterator()
|
iter = NewIterator(ConfigOfDefault)
|
||||||
any.iter = iter
|
any.iter = iter
|
||||||
}
|
}
|
||||||
iter.ResetBytes(any.buf)
|
iter.ResetBytes(any.buf)
|
||||||
|
@ -21,7 +21,7 @@ func (any *uint64LazyAny) ValueType() ValueType {
|
|||||||
func (any *uint64LazyAny) Parse() *Iterator {
|
func (any *uint64LazyAny) Parse() *Iterator {
|
||||||
iter := any.iter
|
iter := any.iter
|
||||||
if iter == nil {
|
if iter == nil {
|
||||||
iter = NewIterator()
|
iter = NewIterator(ConfigOfDefault)
|
||||||
}
|
}
|
||||||
iter.ResetBytes(any.buf)
|
iter.ResetBytes(any.buf)
|
||||||
return iter
|
return iter
|
||||||
|
239
feature_config.go
Normal file
239
feature_config.go
Normal file
@ -0,0 +1,239 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"reflect"
|
||||||
|
"sync/atomic"
|
||||||
|
"unsafe"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
IndentionStep int
|
||||||
|
MarshalFloatWith6Digits bool
|
||||||
|
SupportUnexportedStructFields bool
|
||||||
|
EscapeHtml bool
|
||||||
|
SortMapKeys bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type frozenConfig struct {
|
||||||
|
configBeforeFrozen Config
|
||||||
|
sortMapKeys bool
|
||||||
|
indentionStep int
|
||||||
|
decoderCache unsafe.Pointer
|
||||||
|
encoderCache unsafe.Pointer
|
||||||
|
extensions []ExtensionFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
var ConfigOfDefault = Config{}.Froze()
|
||||||
|
|
||||||
|
// Trying to be 100% compatible with standard library behavior
|
||||||
|
var ConfigCompatibleWithStandardLibrary = Config{
|
||||||
|
EscapeHtml: true,
|
||||||
|
SortMapKeys: true,
|
||||||
|
}.Froze()
|
||||||
|
|
||||||
|
func (cfg Config) Froze() *frozenConfig {
|
||||||
|
frozenConfig := &frozenConfig{
|
||||||
|
sortMapKeys: cfg.SortMapKeys,
|
||||||
|
indentionStep: cfg.IndentionStep,
|
||||||
|
}
|
||||||
|
atomic.StorePointer(&frozenConfig.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
|
||||||
|
atomic.StorePointer(&frozenConfig.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
|
||||||
|
if cfg.MarshalFloatWith6Digits {
|
||||||
|
frozenConfig.marshalFloatWith6Digits()
|
||||||
|
}
|
||||||
|
if cfg.SupportUnexportedStructFields {
|
||||||
|
frozenConfig.supportUnexportedStructFields()
|
||||||
|
}
|
||||||
|
if cfg.EscapeHtml {
|
||||||
|
frozenConfig.escapeHtml()
|
||||||
|
}
|
||||||
|
frozenConfig.configBeforeFrozen = cfg
|
||||||
|
return frozenConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterExtension can register a custom extension
|
||||||
|
func (cfg *frozenConfig) registerExtension(extension ExtensionFunc) {
|
||||||
|
cfg.extensions = append(cfg.extensions, extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) supportUnexportedStructFields() {
|
||||||
|
cfg.registerExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
|
||||||
|
return []string{field.Name}, nil, nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableLossyFloatMarshalling keeps 10**(-6) precision
|
||||||
|
// for float variables for better performance.
|
||||||
|
func (cfg *frozenConfig) marshalFloatWith6Digits() {
|
||||||
|
// for better performance
|
||||||
|
cfg.addEncoderToCache(reflect.TypeOf((*float32)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
val := *((*float32)(ptr))
|
||||||
|
stream.WriteFloat32Lossy(val)
|
||||||
|
}})
|
||||||
|
cfg.addEncoderToCache(reflect.TypeOf((*float64)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
val := *((*float64)(ptr))
|
||||||
|
stream.WriteFloat64Lossy(val)
|
||||||
|
}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) escapeHtml() {
|
||||||
|
// for better performance
|
||||||
|
cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &funcEncoder{func(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
val := *((*string)(ptr))
|
||||||
|
stream.WriteStringWithHtmlEscaped(val)
|
||||||
|
}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
|
||||||
|
done := false
|
||||||
|
for !done {
|
||||||
|
ptr := atomic.LoadPointer(&cfg.decoderCache)
|
||||||
|
cache := *(*map[reflect.Type]Decoder)(ptr)
|
||||||
|
copied := map[reflect.Type]Decoder{}
|
||||||
|
for k, v := range cache {
|
||||||
|
copied[k] = v
|
||||||
|
}
|
||||||
|
copied[cacheKey] = decoder
|
||||||
|
done = atomic.CompareAndSwapPointer(&cfg.decoderCache, ptr, unsafe.Pointer(&copied))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder Encoder) {
|
||||||
|
done := false
|
||||||
|
for !done {
|
||||||
|
ptr := atomic.LoadPointer(&cfg.encoderCache)
|
||||||
|
cache := *(*map[reflect.Type]Encoder)(ptr)
|
||||||
|
copied := map[reflect.Type]Encoder{}
|
||||||
|
for k, v := range cache {
|
||||||
|
copied[k] = v
|
||||||
|
}
|
||||||
|
copied[cacheKey] = encoder
|
||||||
|
done = atomic.CompareAndSwapPointer(&cfg.encoderCache, ptr, unsafe.Pointer(&copied))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) Decoder {
|
||||||
|
ptr := atomic.LoadPointer(&cfg.decoderCache)
|
||||||
|
cache := *(*map[reflect.Type]Decoder)(ptr)
|
||||||
|
return cache[cacheKey]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) Encoder {
|
||||||
|
ptr := atomic.LoadPointer(&cfg.encoderCache)
|
||||||
|
cache := *(*map[reflect.Type]Encoder)(ptr)
|
||||||
|
return cache[cacheKey]
|
||||||
|
}
|
||||||
|
|
||||||
|
// CleanDecoders cleans decoders registered or cached
|
||||||
|
func (cfg *frozenConfig) CleanDecoders() {
|
||||||
|
typeDecoders = map[string]Decoder{}
|
||||||
|
fieldDecoders = map[string]Decoder{}
|
||||||
|
atomic.StorePointer(&cfg.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// CleanEncoders cleans encoders registered or cached
|
||||||
|
func (cfg *frozenConfig) CleanEncoders() {
|
||||||
|
typeEncoders = map[string]Encoder{}
|
||||||
|
fieldEncoders = map[string]Encoder{}
|
||||||
|
atomic.StorePointer(&cfg.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {
|
||||||
|
buf, err := cfg.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(buf), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) Marshal(v interface{}) ([]byte, error) {
|
||||||
|
stream := NewStream(cfg, nil, 256)
|
||||||
|
stream.WriteVal(v)
|
||||||
|
if stream.Error != nil {
|
||||||
|
return nil, stream.Error
|
||||||
|
}
|
||||||
|
return stream.Buffer(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) UnmarshalFromString(str string, v interface{}) error {
|
||||||
|
data := []byte(str)
|
||||||
|
data = data[:lastNotSpacePos(data)]
|
||||||
|
iter := ParseBytes(cfg, data)
|
||||||
|
iter.ReadVal(v)
|
||||||
|
if iter.head == iter.tail {
|
||||||
|
iter.loadMore()
|
||||||
|
}
|
||||||
|
if iter.Error == io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if iter.Error == nil {
|
||||||
|
iter.reportError("UnmarshalFromString", "there are bytes left after unmarshal")
|
||||||
|
}
|
||||||
|
return iter.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) NewEncoder(writer io.Writer) *AdaptedEncoder {
|
||||||
|
stream := NewStream(cfg, writer, 512)
|
||||||
|
return &AdaptedEncoder{stream}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) NewDecoder(reader io.Reader) *AdaptedDecoder {
|
||||||
|
iter := Parse(cfg, reader, 512)
|
||||||
|
return &AdaptedDecoder{iter}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) UnmarshalAnyFromString(str string) (Any, error) {
|
||||||
|
data := []byte(str)
|
||||||
|
data = data[:lastNotSpacePos(data)]
|
||||||
|
iter := ParseBytes(cfg, data)
|
||||||
|
any := iter.ReadAny()
|
||||||
|
if iter.head == iter.tail {
|
||||||
|
iter.loadMore()
|
||||||
|
}
|
||||||
|
if iter.Error == io.EOF {
|
||||||
|
return any, nil
|
||||||
|
}
|
||||||
|
if iter.Error == nil {
|
||||||
|
iter.reportError("UnmarshalAnyFromString", "there are bytes left after unmarshal")
|
||||||
|
}
|
||||||
|
return nil, iter.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) UnmarshalAny(data []byte) (Any, error) {
|
||||||
|
data = data[:lastNotSpacePos(data)]
|
||||||
|
iter := ParseBytes(cfg, data)
|
||||||
|
any := iter.ReadAny()
|
||||||
|
if iter.head == iter.tail {
|
||||||
|
iter.loadMore()
|
||||||
|
}
|
||||||
|
if iter.Error == io.EOF {
|
||||||
|
return any, nil
|
||||||
|
}
|
||||||
|
if iter.Error == nil {
|
||||||
|
iter.reportError("UnmarshalAny", "there are bytes left after unmarshal")
|
||||||
|
}
|
||||||
|
return any, iter.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *frozenConfig) Unmarshal(data []byte, v interface{}) error {
|
||||||
|
data = data[:lastNotSpacePos(data)]
|
||||||
|
iter := ParseBytes(cfg, data)
|
||||||
|
typ := reflect.TypeOf(v)
|
||||||
|
if typ.Kind() != reflect.Ptr {
|
||||||
|
// return non-pointer error
|
||||||
|
return errors.New("the second param must be ptr type")
|
||||||
|
}
|
||||||
|
iter.ReadVal(v)
|
||||||
|
if iter.head == iter.tail {
|
||||||
|
iter.loadMore()
|
||||||
|
}
|
||||||
|
if iter.Error == io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if iter.Error == nil {
|
||||||
|
iter.reportError("Unmarshal", "there are bytes left after unmarshal")
|
||||||
|
}
|
||||||
|
return iter.Error
|
||||||
|
}
|
@ -66,6 +66,7 @@ func init() {
|
|||||||
|
|
||||||
// Iterator is a fast and flexible JSON parser
|
// Iterator is a fast and flexible JSON parser
|
||||||
type Iterator struct {
|
type Iterator struct {
|
||||||
|
cfg *frozenConfig
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
buf []byte
|
buf []byte
|
||||||
head int
|
head int
|
||||||
@ -74,8 +75,9 @@ type Iterator struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create creates an empty Iterator instance
|
// Create creates an empty Iterator instance
|
||||||
func NewIterator() *Iterator {
|
func NewIterator(cfg *frozenConfig) *Iterator {
|
||||||
return &Iterator{
|
return &Iterator{
|
||||||
|
cfg: cfg,
|
||||||
reader: nil,
|
reader: nil,
|
||||||
buf: nil,
|
buf: nil,
|
||||||
head: 0,
|
head: 0,
|
||||||
@ -84,8 +86,9 @@ func NewIterator() *Iterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses a json buffer in io.Reader into an Iterator instance
|
// Parse parses a json buffer in io.Reader into an Iterator instance
|
||||||
func Parse(reader io.Reader, bufSize int) *Iterator {
|
func Parse(cfg *frozenConfig, reader io.Reader, bufSize int) *Iterator {
|
||||||
return &Iterator{
|
return &Iterator{
|
||||||
|
cfg: cfg,
|
||||||
reader: reader,
|
reader: reader,
|
||||||
buf: make([]byte, bufSize),
|
buf: make([]byte, bufSize),
|
||||||
head: 0,
|
head: 0,
|
||||||
@ -94,8 +97,9 @@ func Parse(reader io.Reader, bufSize int) *Iterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ParseBytes parses a json byte slice into an Iterator instance
|
// ParseBytes parses a json byte slice into an Iterator instance
|
||||||
func ParseBytes(input []byte) *Iterator {
|
func ParseBytes(cfg *frozenConfig, input []byte) *Iterator {
|
||||||
return &Iterator{
|
return &Iterator{
|
||||||
|
cfg: cfg,
|
||||||
reader: nil,
|
reader: nil,
|
||||||
buf: input,
|
buf: input,
|
||||||
head: 0,
|
head: 0,
|
||||||
@ -104,8 +108,8 @@ func ParseBytes(input []byte) *Iterator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ParseString parses a json string into an Iterator instance
|
// ParseString parses a json string into an Iterator instance
|
||||||
func ParseString(input string) *Iterator {
|
func ParseString(cfg *frozenConfig, input string) *Iterator {
|
||||||
return ParseBytes([]byte(input))
|
return ParseBytes(cfg, []byte(input))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset can reset an Iterator instance for another json buffer in io.Reader
|
// Reset can reset an Iterator instance for another json buffer in io.Reader
|
||||||
|
@ -102,9 +102,9 @@ non_decimal_loop:
|
|||||||
ind := floatDigits[c]
|
ind := floatDigits[c]
|
||||||
switch ind {
|
switch ind {
|
||||||
case endOfNumber:
|
case endOfNumber:
|
||||||
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
|
if decimalPlaces > 0 && decimalPlaces < len(_POW10) {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return float32(float64(value) / float64(POW10[decimalPlaces]))
|
return float32(float64(value) / float64(_POW10[decimalPlaces]))
|
||||||
}
|
}
|
||||||
// too many decimal places
|
// too many decimal places
|
||||||
return iter.readFloat32SlowPath()
|
return iter.readFloat32SlowPath()
|
||||||
@ -205,9 +205,9 @@ non_decimal_loop:
|
|||||||
ind := floatDigits[c]
|
ind := floatDigits[c]
|
||||||
switch ind {
|
switch ind {
|
||||||
case endOfNumber:
|
case endOfNumber:
|
||||||
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
|
if decimalPlaces > 0 && decimalPlaces < len(_POW10) {
|
||||||
iter.head = i
|
iter.head = i
|
||||||
return float64(value) / float64(POW10[decimalPlaces])
|
return float64(value) / float64(_POW10[decimalPlaces])
|
||||||
}
|
}
|
||||||
// too many decimal places
|
// too many decimal places
|
||||||
return iter.readFloat64SlowPath()
|
return iter.readFloat64SlowPath()
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync/atomic"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,6 +33,10 @@ type Encoder interface {
|
|||||||
|
|
||||||
func writeToStream(val interface{}, stream *Stream, encoder Encoder) {
|
func writeToStream(val interface{}, stream *Stream, encoder Encoder) {
|
||||||
e := (*emptyInterface)(unsafe.Pointer(&val))
|
e := (*emptyInterface)(unsafe.Pointer(&val))
|
||||||
|
if e.word == nil {
|
||||||
|
stream.WriteNil()
|
||||||
|
return
|
||||||
|
}
|
||||||
if reflect.TypeOf(val).Kind() == reflect.Ptr {
|
if reflect.TypeOf(val).Kind() == reflect.Ptr {
|
||||||
encoder.encode(unsafe.Pointer(&e.word), stream)
|
encoder.encode(unsafe.Pointer(&e.word), stream)
|
||||||
} else {
|
} else {
|
||||||
@ -69,9 +72,6 @@ func (encoder *funcEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var DECODERS unsafe.Pointer
|
|
||||||
var ENCODERS unsafe.Pointer
|
|
||||||
|
|
||||||
var typeDecoders map[string]Decoder
|
var typeDecoders map[string]Decoder
|
||||||
var fieldDecoders map[string]Decoder
|
var fieldDecoders map[string]Decoder
|
||||||
var typeEncoders map[string]Encoder
|
var typeEncoders map[string]Encoder
|
||||||
@ -90,8 +90,6 @@ func init() {
|
|||||||
typeEncoders = map[string]Encoder{}
|
typeEncoders = map[string]Encoder{}
|
||||||
fieldEncoders = map[string]Encoder{}
|
fieldEncoders = map[string]Encoder{}
|
||||||
extensions = []ExtensionFunc{}
|
extensions = []ExtensionFunc{}
|
||||||
atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{}))
|
|
||||||
atomic.StorePointer(&ENCODERS, unsafe.Pointer(&map[string]Encoder{}))
|
|
||||||
jsonNumberType = reflect.TypeOf((*json.Number)(nil)).Elem()
|
jsonNumberType = reflect.TypeOf((*json.Number)(nil)).Elem()
|
||||||
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
|
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
|
||||||
anyType = reflect.TypeOf((*Any)(nil)).Elem()
|
anyType = reflect.TypeOf((*Any)(nil)).Elem()
|
||||||
@ -100,46 +98,6 @@ func init() {
|
|||||||
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
func addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
|
|
||||||
done := false
|
|
||||||
for !done {
|
|
||||||
ptr := atomic.LoadPointer(&DECODERS)
|
|
||||||
cache := *(*map[reflect.Type]Decoder)(ptr)
|
|
||||||
copied := map[reflect.Type]Decoder{}
|
|
||||||
for k, v := range cache {
|
|
||||||
copied[k] = v
|
|
||||||
}
|
|
||||||
copied[cacheKey] = decoder
|
|
||||||
done = atomic.CompareAndSwapPointer(&DECODERS, ptr, unsafe.Pointer(&copied))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func addEncoderToCache(cacheKey reflect.Type, encoder Encoder) {
|
|
||||||
done := false
|
|
||||||
for !done {
|
|
||||||
ptr := atomic.LoadPointer(&ENCODERS)
|
|
||||||
cache := *(*map[reflect.Type]Encoder)(ptr)
|
|
||||||
copied := map[reflect.Type]Encoder{}
|
|
||||||
for k, v := range cache {
|
|
||||||
copied[k] = v
|
|
||||||
}
|
|
||||||
copied[cacheKey] = encoder
|
|
||||||
done = atomic.CompareAndSwapPointer(&ENCODERS, ptr, unsafe.Pointer(&copied))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDecoderFromCache(cacheKey reflect.Type) Decoder {
|
|
||||||
ptr := atomic.LoadPointer(&DECODERS)
|
|
||||||
cache := *(*map[reflect.Type]Decoder)(ptr)
|
|
||||||
return cache[cacheKey]
|
|
||||||
}
|
|
||||||
|
|
||||||
func getEncoderFromCache(cacheKey reflect.Type) Encoder {
|
|
||||||
ptr := atomic.LoadPointer(&ENCODERS)
|
|
||||||
cache := *(*map[reflect.Type]Encoder)(ptr)
|
|
||||||
return cache[cacheKey]
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterTypeDecoder can register a type for json object
|
// RegisterTypeDecoder can register a type for json object
|
||||||
func RegisterTypeDecoder(typ string, fun DecoderFunc) {
|
func RegisterTypeDecoder(typ string, fun DecoderFunc) {
|
||||||
typeDecoders[typ] = &funcDecoder{fun}
|
typeDecoders[typ] = &funcDecoder{fun}
|
||||||
@ -163,20 +121,6 @@ func RegisterExtension(extension ExtensionFunc) {
|
|||||||
extensions = append(extensions, extension)
|
extensions = append(extensions, extension)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CleanDecoders cleans decoders registered or cached
|
|
||||||
func CleanDecoders() {
|
|
||||||
typeDecoders = map[string]Decoder{}
|
|
||||||
fieldDecoders = map[string]Decoder{}
|
|
||||||
atomic.StorePointer(&DECODERS, unsafe.Pointer(&map[string]Decoder{}))
|
|
||||||
}
|
|
||||||
|
|
||||||
// CleanEncoders cleans encoders registered or cached
|
|
||||||
func CleanEncoders() {
|
|
||||||
typeEncoders = map[string]Encoder{}
|
|
||||||
fieldEncoders = map[string]Encoder{}
|
|
||||||
atomic.StorePointer(&ENCODERS, unsafe.Pointer(&map[string]Encoder{}))
|
|
||||||
}
|
|
||||||
|
|
||||||
type optionalDecoder struct {
|
type optionalDecoder struct {
|
||||||
valueType reflect.Type
|
valueType reflect.Type
|
||||||
valueDecoder Decoder
|
valueDecoder Decoder
|
||||||
@ -270,15 +214,15 @@ type nonEmptyInterface struct {
|
|||||||
func (iter *Iterator) ReadVal(obj interface{}) {
|
func (iter *Iterator) ReadVal(obj interface{}) {
|
||||||
typ := reflect.TypeOf(obj)
|
typ := reflect.TypeOf(obj)
|
||||||
cacheKey := typ.Elem()
|
cacheKey := typ.Elem()
|
||||||
cachedDecoder := getDecoderFromCache(cacheKey)
|
cachedDecoder := iter.cfg.getDecoderFromCache(cacheKey)
|
||||||
if cachedDecoder == nil {
|
if cachedDecoder == nil {
|
||||||
decoder, err := decoderOfType(cacheKey)
|
decoder, err := decoderOfType(iter.cfg, cacheKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
iter.Error = err
|
iter.Error = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cachedDecoder = decoder
|
cachedDecoder = decoder
|
||||||
addDecoderToCache(cacheKey, decoder)
|
iter.cfg.addDecoderToCache(cacheKey, decoder)
|
||||||
}
|
}
|
||||||
e := (*emptyInterface)(unsafe.Pointer(&obj))
|
e := (*emptyInterface)(unsafe.Pointer(&obj))
|
||||||
cachedDecoder.decode(e.word, iter)
|
cachedDecoder.decode(e.word, iter)
|
||||||
@ -291,15 +235,15 @@ func (stream *Stream) WriteVal(val interface{}) {
|
|||||||
}
|
}
|
||||||
typ := reflect.TypeOf(val)
|
typ := reflect.TypeOf(val)
|
||||||
cacheKey := typ
|
cacheKey := typ
|
||||||
cachedEncoder := getEncoderFromCache(cacheKey)
|
cachedEncoder := stream.cfg.getEncoderFromCache(cacheKey)
|
||||||
if cachedEncoder == nil {
|
if cachedEncoder == nil {
|
||||||
encoder, err := encoderOfType(cacheKey)
|
encoder, err := encoderOfType(stream.cfg, cacheKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
stream.Error = err
|
stream.Error = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cachedEncoder = encoder
|
cachedEncoder = encoder
|
||||||
addEncoderToCache(cacheKey, encoder)
|
stream.cfg.addEncoderToCache(cacheKey, encoder)
|
||||||
}
|
}
|
||||||
cachedEncoder.encodeInterface(val, stream)
|
cachedEncoder.encodeInterface(val, stream)
|
||||||
}
|
}
|
||||||
@ -320,7 +264,7 @@ func (p prefix) addToEncoder(encoder Encoder, err error) (Encoder, error) {
|
|||||||
return encoder, err
|
return encoder, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func decoderOfType(typ reflect.Type) (Decoder, error) {
|
func decoderOfType(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
||||||
typeName := typ.String()
|
typeName := typ.String()
|
||||||
typeDecoder := typeDecoders[typeName]
|
typeDecoder := typeDecoders[typeName]
|
||||||
if typeDecoder != nil {
|
if typeDecoder != nil {
|
||||||
@ -333,19 +277,19 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
cacheKey := typ
|
cacheKey := typ
|
||||||
cachedDecoder := getDecoderFromCache(cacheKey)
|
cachedDecoder := cfg.getDecoderFromCache(cacheKey)
|
||||||
if cachedDecoder != nil {
|
if cachedDecoder != nil {
|
||||||
return cachedDecoder, nil
|
return cachedDecoder, nil
|
||||||
}
|
}
|
||||||
placeholder := &placeholderDecoder{}
|
placeholder := &placeholderDecoder{}
|
||||||
addDecoderToCache(cacheKey, placeholder)
|
cfg.addDecoderToCache(cacheKey, placeholder)
|
||||||
newDecoder, err := createDecoderOfType(typ)
|
newDecoder, err := createDecoderOfType(cfg, typ)
|
||||||
placeholder.valueDecoder = newDecoder
|
placeholder.valueDecoder = newDecoder
|
||||||
addDecoderToCache(cacheKey, newDecoder)
|
cfg.addDecoderToCache(cacheKey, newDecoder)
|
||||||
return newDecoder, err
|
return newDecoder, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func createDecoderOfType(typ reflect.Type) (Decoder, error) {
|
func createDecoderOfType(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
||||||
if typ.String() == "[]uint8" {
|
if typ.String() == "[]uint8" {
|
||||||
return &base64Codec{}, nil
|
return &base64Codec{}, nil
|
||||||
}
|
}
|
||||||
@ -398,19 +342,19 @@ func createDecoderOfType(typ reflect.Type) (Decoder, error) {
|
|||||||
return &nonEmptyInterfaceCodec{}, nil
|
return &nonEmptyInterfaceCodec{}, nil
|
||||||
}
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
return prefix(fmt.Sprintf("[%s]", typ.String())).addToDecoder(decoderOfStruct(typ))
|
return prefix(fmt.Sprintf("[%s]", typ.String())).addToDecoder(decoderOfStruct(cfg, typ))
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
return prefix("[slice]").addToDecoder(decoderOfSlice(typ))
|
return prefix("[slice]").addToDecoder(decoderOfSlice(cfg, typ))
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
return prefix("[map]").addToDecoder(decoderOfMap(typ))
|
return prefix("[map]").addToDecoder(decoderOfMap(cfg, typ))
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
return prefix("[optional]").addToDecoder(decoderOfOptional(typ))
|
return prefix("[optional]").addToDecoder(decoderOfOptional(cfg, typ))
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported type: %v", typ)
|
return nil, fmt.Errorf("unsupported type: %v", typ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func encoderOfType(typ reflect.Type) (Encoder, error) {
|
func encoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
|
||||||
typeName := typ.String()
|
typeName := typ.String()
|
||||||
typeEncoder := typeEncoders[typeName]
|
typeEncoder := typeEncoders[typeName]
|
||||||
if typeEncoder != nil {
|
if typeEncoder != nil {
|
||||||
@ -423,19 +367,19 @@ func encoderOfType(typ reflect.Type) (Encoder, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
cacheKey := typ
|
cacheKey := typ
|
||||||
cachedEncoder := getEncoderFromCache(cacheKey)
|
cachedEncoder := cfg.getEncoderFromCache(cacheKey)
|
||||||
if cachedEncoder != nil {
|
if cachedEncoder != nil {
|
||||||
return cachedEncoder, nil
|
return cachedEncoder, nil
|
||||||
}
|
}
|
||||||
placeholder := &placeholderEncoder{}
|
placeholder := &placeholderEncoder{}
|
||||||
addEncoderToCache(cacheKey, placeholder)
|
cfg.addEncoderToCache(cacheKey, placeholder)
|
||||||
newEncoder, err := createEncoderOfType(typ)
|
newEncoder, err := createEncoderOfType(cfg, typ)
|
||||||
placeholder.valueEncoder = newEncoder
|
placeholder.valueEncoder = newEncoder
|
||||||
addEncoderToCache(cacheKey, newEncoder)
|
cfg.addEncoderToCache(cacheKey, newEncoder)
|
||||||
return newEncoder, err
|
return newEncoder, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func createEncoderOfType(typ reflect.Type) (Encoder, error) {
|
func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
|
||||||
if typ.String() == "[]uint8" {
|
if typ.String() == "[]uint8" {
|
||||||
return &base64Codec{}, nil
|
return &base64Codec{}, nil
|
||||||
}
|
}
|
||||||
@ -489,38 +433,42 @@ func createEncoderOfType(typ reflect.Type) (Encoder, error) {
|
|||||||
return &nonEmptyInterfaceCodec{}, nil
|
return &nonEmptyInterfaceCodec{}, nil
|
||||||
}
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
return prefix(fmt.Sprintf("[%s]", typ.String())).addToEncoder(encoderOfStruct(typ))
|
return prefix(fmt.Sprintf("[%s]", typ.String())).addToEncoder(encoderOfStruct(cfg, typ))
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
return prefix("[slice]").addToEncoder(encoderOfSlice(typ))
|
return prefix("[slice]").addToEncoder(encoderOfSlice(cfg, typ))
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
return prefix("[map]").addToEncoder(encoderOfMap(typ))
|
return prefix("[map]").addToEncoder(encoderOfMap(cfg, typ))
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
return prefix("[optional]").addToEncoder(encoderOfOptional(typ))
|
return prefix("[optional]").addToEncoder(encoderOfOptional(cfg, typ))
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported type: %v", typ)
|
return nil, fmt.Errorf("unsupported type: %v", typ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func decoderOfOptional(typ reflect.Type) (Decoder, error) {
|
func decoderOfOptional(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
||||||
elemType := typ.Elem()
|
elemType := typ.Elem()
|
||||||
decoder, err := decoderOfType(elemType)
|
decoder, err := decoderOfType(cfg, elemType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &optionalDecoder{elemType, decoder}, nil
|
return &optionalDecoder{elemType, decoder}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func encoderOfOptional(typ reflect.Type) (Encoder, error) {
|
func encoderOfOptional(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
|
||||||
elemType := typ.Elem()
|
elemType := typ.Elem()
|
||||||
decoder, err := encoderOfType(elemType)
|
elemEncoder, err := encoderOfType(cfg, elemType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &optionalEncoder{decoder}, nil
|
encoder := &optionalEncoder{elemEncoder}
|
||||||
|
if elemType.Kind() == reflect.Map {
|
||||||
|
encoder = &optionalEncoder{encoder}
|
||||||
|
}
|
||||||
|
return encoder, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func decoderOfMap(typ reflect.Type) (Decoder, error) {
|
func decoderOfMap(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
||||||
decoder, err := decoderOfType(typ.Elem())
|
decoder, err := decoderOfType(cfg, typ.Elem())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -532,12 +480,16 @@ func extractInterface(val interface{}) emptyInterface {
|
|||||||
return *((*emptyInterface)(unsafe.Pointer(&val)))
|
return *((*emptyInterface)(unsafe.Pointer(&val)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func encoderOfMap(typ reflect.Type) (Encoder, error) {
|
func encoderOfMap(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
|
||||||
elemType := typ.Elem()
|
elemType := typ.Elem()
|
||||||
encoder, err := encoderOfType(elemType)
|
encoder, err := encoderOfType(cfg, elemType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
mapInterface := reflect.New(typ).Elem().Interface()
|
mapInterface := reflect.New(typ).Elem().Interface()
|
||||||
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
if cfg.sortMapKeys {
|
||||||
|
return &sortKeysMapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
||||||
|
} else {
|
||||||
|
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,16 +7,16 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func decoderOfSlice(typ reflect.Type) (Decoder, error) {
|
func decoderOfSlice(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
||||||
decoder, err := decoderOfType(typ.Elem())
|
decoder, err := decoderOfType(cfg, typ.Elem())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &sliceDecoder{typ, typ.Elem(), decoder}, nil
|
return &sliceDecoder{typ, typ.Elem(), decoder}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func encoderOfSlice(typ reflect.Type) (Encoder, error) {
|
func encoderOfSlice(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
|
||||||
encoder, err := encoderOfType(typ.Elem())
|
encoder, err := encoderOfType(cfg, typ.Elem())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -34,6 +34,10 @@ type sliceEncoder struct {
|
|||||||
|
|
||||||
func (encoder *sliceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
func (encoder *sliceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
slice := (*sliceHeader)(ptr)
|
slice := (*sliceHeader)(ptr)
|
||||||
|
if slice.Data == nil {
|
||||||
|
stream.WriteNil()
|
||||||
|
return
|
||||||
|
}
|
||||||
if slice.Len == 0 {
|
if slice.Len == 0 {
|
||||||
stream.WriteEmptyArray()
|
stream.WriteEmptyArray()
|
||||||
return
|
return
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mapDecoder struct {
|
type mapDecoder struct {
|
||||||
@ -136,3 +137,81 @@ func (encoder *mapEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
|||||||
realVal := reflect.ValueOf(*realInterface)
|
realVal := reflect.ValueOf(*realInterface)
|
||||||
return realVal.Len() == 0
|
return realVal.Len() == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type sortKeysMapEncoder struct {
|
||||||
|
mapType reflect.Type
|
||||||
|
elemType reflect.Type
|
||||||
|
elemEncoder Encoder
|
||||||
|
mapInterface emptyInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *sortKeysMapEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
mapInterface := encoder.mapInterface
|
||||||
|
mapInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
||||||
|
realVal := reflect.ValueOf(*realInterface)
|
||||||
|
|
||||||
|
|
||||||
|
// Extract and sort the keys.
|
||||||
|
keys := realVal.MapKeys()
|
||||||
|
sv := make([]reflectWithString, len(keys))
|
||||||
|
for i, v := range keys {
|
||||||
|
sv[i].v = v
|
||||||
|
if err := sv[i].resolve(); err != nil {
|
||||||
|
stream.Error = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Slice(sv, func(i, j int) bool { return sv[i].s < sv[j].s })
|
||||||
|
|
||||||
|
stream.WriteObjectStart()
|
||||||
|
for i, key := range sv {
|
||||||
|
if i != 0 {
|
||||||
|
stream.WriteMore()
|
||||||
|
}
|
||||||
|
encodeMapKey(key.v, stream)
|
||||||
|
stream.writeByte(':')
|
||||||
|
val := realVal.MapIndex(key.v).Interface()
|
||||||
|
encoder.elemEncoder.encodeInterface(val, stream)
|
||||||
|
}
|
||||||
|
stream.WriteObjectEnd()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type reflectWithString struct {
|
||||||
|
v reflect.Value
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *reflectWithString) resolve() error {
|
||||||
|
if w.v.Kind() == reflect.String {
|
||||||
|
w.s = w.v.String()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if tm, ok := w.v.Interface().(encoding.TextMarshaler); ok {
|
||||||
|
buf, err := tm.MarshalText()
|
||||||
|
w.s = string(buf)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
switch w.v.Kind() {
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
w.s = strconv.FormatInt(w.v.Int(), 10)
|
||||||
|
return nil
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
|
w.s = strconv.FormatUint(w.v.Uint(), 10)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
panic("unexpected map key type")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *sortKeysMapEncoder) encodeInterface(val interface{}, stream *Stream) {
|
||||||
|
writeToStream(val, stream, encoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (encoder *sortKeysMapEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||||
|
mapInterface := encoder.mapInterface
|
||||||
|
mapInterface.word = ptr
|
||||||
|
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
|
||||||
|
realVal := reflect.ValueOf(*realInterface)
|
||||||
|
return realVal.Len() == 0
|
||||||
|
}
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func encoderOfStruct(typ reflect.Type) (Encoder, error) {
|
func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
|
||||||
structEncoder_ := &structEncoder{}
|
structEncoder_ := &structEncoder{}
|
||||||
fields := map[string]*structFieldEncoder{}
|
fields := map[string]*structFieldEncoder{}
|
||||||
for _, field := range listStructFields(typ) {
|
for _, field := range listStructFields(typ) {
|
||||||
@ -24,6 +24,15 @@ func encoderOfStruct(typ reflect.Type) (Encoder, error) {
|
|||||||
fieldEncoders[fieldEncoderKey] = &funcEncoder{fun}
|
fieldEncoders[fieldEncoderKey] = &funcEncoder{fun}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, extension := range cfg.extensions {
|
||||||
|
alternativeFieldNames, fun, _ := extension(typ, field)
|
||||||
|
if alternativeFieldNames != nil {
|
||||||
|
extensionProvidedFieldNames = alternativeFieldNames
|
||||||
|
}
|
||||||
|
if fun != nil {
|
||||||
|
fieldEncoders[fieldEncoderKey] = &funcEncoder{fun}
|
||||||
|
}
|
||||||
|
}
|
||||||
tagParts := strings.Split(field.Tag.Get("json"), ",")
|
tagParts := strings.Split(field.Tag.Get("json"), ",")
|
||||||
// if fieldNames set by extension, use theirs, otherwise try tags
|
// if fieldNames set by extension, use theirs, otherwise try tags
|
||||||
fieldNames := calcFieldNames(field.Name, tagParts[0], extensionProvidedFieldNames)
|
fieldNames := calcFieldNames(field.Name, tagParts[0], extensionProvidedFieldNames)
|
||||||
@ -36,7 +45,7 @@ func encoderOfStruct(typ reflect.Type) (Encoder, error) {
|
|||||||
encoder := fieldEncoders[fieldEncoderKey]
|
encoder := fieldEncoders[fieldEncoderKey]
|
||||||
var err error
|
var err error
|
||||||
if encoder == nil && len(fieldNames) > 0 {
|
if encoder == nil && len(fieldNames) > 0 {
|
||||||
encoder, err = encoderOfType(field.Type)
|
encoder, err = encoderOfType(cfg, field.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return prefix(fmt.Sprintf("{%s}", field.Name)).addToEncoder(encoder, err)
|
return prefix(fmt.Sprintf("{%s}", field.Name)).addToEncoder(encoder, err)
|
||||||
}
|
}
|
||||||
@ -71,7 +80,7 @@ func listStructFields(typ reflect.Type) []*reflect.StructField {
|
|||||||
return fields
|
return fields
|
||||||
}
|
}
|
||||||
|
|
||||||
func decoderOfStruct(typ reflect.Type) (Decoder, error) {
|
func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
|
||||||
fields := map[string]*structFieldDecoder{}
|
fields := map[string]*structFieldDecoder{}
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
for i := 0; i < typ.NumField(); i++ {
|
||||||
field := typ.Field(i)
|
field := typ.Field(i)
|
||||||
@ -86,12 +95,21 @@ func decoderOfStruct(typ reflect.Type) (Decoder, error) {
|
|||||||
fieldDecoders[fieldDecoderKey] = &funcDecoder{fun}
|
fieldDecoders[fieldDecoderKey] = &funcDecoder{fun}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, extension := range cfg.extensions {
|
||||||
|
alternativeFieldNames, _, fun := extension(typ, &field)
|
||||||
|
if alternativeFieldNames != nil {
|
||||||
|
extensionProviedFieldNames = alternativeFieldNames
|
||||||
|
}
|
||||||
|
if fun != nil {
|
||||||
|
fieldDecoders[fieldDecoderKey] = &funcDecoder{fun}
|
||||||
|
}
|
||||||
|
}
|
||||||
decoder := fieldDecoders[fieldDecoderKey]
|
decoder := fieldDecoders[fieldDecoderKey]
|
||||||
tagParts := strings.Split(field.Tag.Get("json"), ",")
|
tagParts := strings.Split(field.Tag.Get("json"), ",")
|
||||||
fieldNames := calcFieldNames(field.Name, tagParts[0], extensionProviedFieldNames)
|
fieldNames := calcFieldNames(field.Name, tagParts[0], extensionProviedFieldNames)
|
||||||
if decoder == nil && len(fieldNames) > 0 {
|
if decoder == nil && len(fieldNames) > 0 {
|
||||||
var err error
|
var err error
|
||||||
decoder, err = decoderOfType(field.Type)
|
decoder, err = decoderOfType(cfg, field.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return prefix(fmt.Sprintf("{%s}", field.Name)).addToDecoder(decoder, err)
|
return prefix(fmt.Sprintf("{%s}", field.Name)).addToDecoder(decoder, err)
|
||||||
}
|
}
|
||||||
@ -130,12 +148,6 @@ func calcFieldNames(originalFieldName string, tagProvidedFieldName string, exten
|
|||||||
return fieldNames
|
return fieldNames
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnableUnexportedStructFieldsSupport() {
|
|
||||||
RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, EncoderFunc, DecoderFunc) {
|
|
||||||
return []string{field.Name}, nil, nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (Decoder, error) {
|
func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (Decoder, error) {
|
||||||
knownHash := map[int32]struct{}{
|
knownHash := map[int32]struct{}{
|
||||||
0: {},
|
0: {},
|
||||||
|
@ -5,16 +5,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Stream struct {
|
type Stream struct {
|
||||||
out io.Writer
|
cfg *frozenConfig
|
||||||
buf []byte
|
out io.Writer
|
||||||
n int
|
buf []byte
|
||||||
Error error
|
n int
|
||||||
indention int
|
Error error
|
||||||
IndentionStep int
|
indention int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStream(out io.Writer, bufSize int) *Stream {
|
func NewStream(cfg *frozenConfig, out io.Writer, bufSize int) *Stream {
|
||||||
return &Stream{out, make([]byte, bufSize), 0, nil, 0, 0}
|
return &Stream{
|
||||||
|
cfg: cfg,
|
||||||
|
out: out,
|
||||||
|
buf: make([]byte, bufSize),
|
||||||
|
n: 0,
|
||||||
|
Error: nil,
|
||||||
|
indention: 0,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Stream) Reset(out io.Writer) {
|
func (b *Stream) Reset(out io.Writer) {
|
||||||
@ -190,64 +197,6 @@ func (b *Stream) WriteRaw(s string) {
|
|||||||
b.n += n
|
b.n += n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteString(s string) {
|
|
||||||
stream.ensure(32)
|
|
||||||
valLen := len(s)
|
|
||||||
toWriteLen := valLen
|
|
||||||
bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
|
|
||||||
if stream.n+toWriteLen > bufLengthMinusTwo {
|
|
||||||
toWriteLen = bufLengthMinusTwo - stream.n
|
|
||||||
}
|
|
||||||
n := stream.n
|
|
||||||
stream.buf[n] = '"'
|
|
||||||
n++
|
|
||||||
// write string, the fast path, without utf8 and escape support
|
|
||||||
i := 0
|
|
||||||
for ; i < toWriteLen; i++ {
|
|
||||||
c := s[i]
|
|
||||||
if c > 31 && c != '"' && c != '\\' {
|
|
||||||
stream.buf[n] = c
|
|
||||||
n++
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if i == valLen {
|
|
||||||
stream.buf[n] = '"'
|
|
||||||
n++
|
|
||||||
stream.n = n
|
|
||||||
return
|
|
||||||
}
|
|
||||||
stream.n = n
|
|
||||||
// for the remaining parts, we process them char by char
|
|
||||||
stream.writeStringSlowPath(s, i, valLen)
|
|
||||||
stream.writeByte('"')
|
|
||||||
}
|
|
||||||
|
|
||||||
func (stream *Stream) writeStringSlowPath(s string, i int, valLen int) {
|
|
||||||
for ; i < valLen; i++ {
|
|
||||||
c := s[i]
|
|
||||||
switch c {
|
|
||||||
case '"':
|
|
||||||
stream.writeTwoBytes('\\', '"')
|
|
||||||
case '\\':
|
|
||||||
stream.writeTwoBytes('\\', '\\')
|
|
||||||
case '\b':
|
|
||||||
stream.writeTwoBytes('\\', 'b')
|
|
||||||
case '\f':
|
|
||||||
stream.writeTwoBytes('\\', 'f')
|
|
||||||
case '\n':
|
|
||||||
stream.writeTwoBytes('\\', 'n')
|
|
||||||
case '\r':
|
|
||||||
stream.writeTwoBytes('\\', 'r')
|
|
||||||
case '\t':
|
|
||||||
stream.writeTwoBytes('\\', 't')
|
|
||||||
default:
|
|
||||||
stream.writeByte(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (stream *Stream) WriteNil() {
|
func (stream *Stream) WriteNil() {
|
||||||
stream.writeFourBytes('n', 'u', 'l', 'l')
|
stream.writeFourBytes('n', 'u', 'l', 'l')
|
||||||
}
|
}
|
||||||
@ -269,7 +218,7 @@ func (stream *Stream) WriteBool(val bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteObjectStart() {
|
func (stream *Stream) WriteObjectStart() {
|
||||||
stream.indention += stream.IndentionStep
|
stream.indention += stream.cfg.indentionStep
|
||||||
stream.writeByte('{')
|
stream.writeByte('{')
|
||||||
stream.writeIndention(0)
|
stream.writeIndention(0)
|
||||||
}
|
}
|
||||||
@ -280,8 +229,8 @@ func (stream *Stream) WriteObjectField(field string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteObjectEnd() {
|
func (stream *Stream) WriteObjectEnd() {
|
||||||
stream.writeIndention(stream.IndentionStep)
|
stream.writeIndention(stream.cfg.indentionStep)
|
||||||
stream.indention -= stream.IndentionStep
|
stream.indention -= stream.cfg.indentionStep
|
||||||
stream.writeByte('}')
|
stream.writeByte('}')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,7 +245,7 @@ func (stream *Stream) WriteMore() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteArrayStart() {
|
func (stream *Stream) WriteArrayStart() {
|
||||||
stream.indention += stream.IndentionStep
|
stream.indention += stream.cfg.indentionStep
|
||||||
stream.writeByte('[')
|
stream.writeByte('[')
|
||||||
stream.writeIndention(0)
|
stream.writeIndention(0)
|
||||||
}
|
}
|
||||||
@ -307,8 +256,8 @@ func (stream *Stream) WriteEmptyArray() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteArrayEnd() {
|
func (stream *Stream) WriteArrayEnd() {
|
||||||
stream.writeIndention(stream.IndentionStep)
|
stream.writeIndention(stream.cfg.indentionStep)
|
||||||
stream.indention -= stream.IndentionStep
|
stream.indention -= stream.cfg.indentionStep
|
||||||
stream.writeByte(']')
|
stream.writeByte(']')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,13 +2,12 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var POW10 []uint64
|
var _POW10 []uint64
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
POW10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
|
_POW10 = []uint64{1, 10, 100, 1000, 10000, 100000, 1000000}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteFloat32(val float32) {
|
func (stream *Stream) WriteFloat32(val float32) {
|
||||||
@ -34,7 +33,7 @@ func (stream *Stream) WriteFloat32Lossy(val float32) {
|
|||||||
}
|
}
|
||||||
stream.writeByte('.')
|
stream.writeByte('.')
|
||||||
stream.ensure(10)
|
stream.ensure(10)
|
||||||
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
|
||||||
stream.writeByte('0')
|
stream.writeByte('0')
|
||||||
}
|
}
|
||||||
stream.WriteUint64(fval)
|
stream.WriteUint64(fval)
|
||||||
@ -66,7 +65,7 @@ func (stream *Stream) WriteFloat64Lossy(val float64) {
|
|||||||
}
|
}
|
||||||
stream.writeByte('.')
|
stream.writeByte('.')
|
||||||
stream.ensure(10)
|
stream.ensure(10)
|
||||||
for p := precision - 1; p > 0 && fval < POW10[p]; p-- {
|
for p := precision - 1; p > 0 && fval < _POW10[p]; p-- {
|
||||||
stream.writeByte('0')
|
stream.writeByte('0')
|
||||||
}
|
}
|
||||||
stream.WriteUint64(fval)
|
stream.WriteUint64(fval)
|
||||||
@ -74,17 +73,3 @@ func (stream *Stream) WriteFloat64Lossy(val float64) {
|
|||||||
stream.n--
|
stream.n--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnableLossyFloatMarshalling keeps 10**(-6) precision
|
|
||||||
// for float variables for better performance.
|
|
||||||
func EnableLossyFloatMarshalling() {
|
|
||||||
// for better performance
|
|
||||||
RegisterTypeEncoder("float32", func(ptr unsafe.Pointer, stream *Stream) {
|
|
||||||
val := *((*float32)(ptr))
|
|
||||||
stream.WriteFloat32Lossy(val)
|
|
||||||
})
|
|
||||||
RegisterTypeEncoder("float64", func(ptr unsafe.Pointer, stream *Stream) {
|
|
||||||
val := *((*float64)(ptr))
|
|
||||||
stream.WriteFloat64Lossy(val)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
var DIGITS []uint32
|
var _DIGITS []uint32
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
DIGITS = make([]uint32, 1000)
|
_DIGITS = make([]uint32, 1000)
|
||||||
for i := uint32(0); i < 1000; i++ {
|
for i := uint32(0); i < 1000; i++ {
|
||||||
DIGITS[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
|
_DIGITS[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
|
||||||
if i < 10 {
|
if i < 10 {
|
||||||
DIGITS[i] += 2 << 24
|
_DIGITS[i] += 2 << 24
|
||||||
} else if i < 100 {
|
} else if i < 100 {
|
||||||
DIGITS[i] += 1 << 24
|
_DIGITS[i] += 1 << 24
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -38,7 +38,7 @@ func writeBuf(buf []byte, v uint32, n int) {
|
|||||||
|
|
||||||
func (stream *Stream) WriteUint8(val uint8) {
|
func (stream *Stream) WriteUint8(val uint8) {
|
||||||
stream.ensure(3)
|
stream.ensure(3)
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], stream.n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteInt8(nval int8) {
|
func (stream *Stream) WriteInt8(nval int8) {
|
||||||
@ -52,19 +52,19 @@ func (stream *Stream) WriteInt8(nval int8) {
|
|||||||
} else {
|
} else {
|
||||||
val = uint8(nval)
|
val = uint8(nval)
|
||||||
}
|
}
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) WriteUint16(val uint16) {
|
func (stream *Stream) WriteUint16(val uint16) {
|
||||||
stream.ensure(5)
|
stream.ensure(5)
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], stream.n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], stream.n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1*1000
|
r1 := val - q1*1000
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], stream.n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], stream.n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -82,12 +82,12 @@ func (stream *Stream) WriteInt16(nval int16) {
|
|||||||
}
|
}
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1*1000
|
r1 := val - q1*1000
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -97,30 +97,30 @@ func (stream *Stream) WriteUint32(val uint32) {
|
|||||||
n := stream.n
|
n := stream.n
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1*1000
|
r1 := val - q1*1000
|
||||||
q2 := q1 / 1000
|
q2 := q1 / 1000
|
||||||
if q2 == 0 {
|
if q2 == 0 {
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r2 := q1 - q2*1000
|
r2 := q1 - q2*1000
|
||||||
q3 := q2 / 1000
|
q3 := q2 / 1000
|
||||||
if q3 == 0 {
|
if q3 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||||
} else {
|
} else {
|
||||||
r3 := q2 - q3*1000
|
r3 := q2 - q3*1000
|
||||||
stream.buf[n] = byte(q3 + '0')
|
stream.buf[n] = byte(q3 + '0')
|
||||||
n++
|
n++
|
||||||
writeBuf(stream.buf, DIGITS[r3], n)
|
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||||
n += 3
|
n += 3
|
||||||
}
|
}
|
||||||
writeBuf(stream.buf, DIGITS[r2], n)
|
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+3)
|
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||||
stream.n = n + 6
|
stream.n = n + 6
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,30 +137,30 @@ func (stream *Stream) WriteInt32(nval int32) {
|
|||||||
}
|
}
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1*1000
|
r1 := val - q1*1000
|
||||||
q2 := q1 / 1000
|
q2 := q1 / 1000
|
||||||
if q2 == 0 {
|
if q2 == 0 {
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r2 := q1 - q2*1000
|
r2 := q1 - q2*1000
|
||||||
q3 := q2 / 1000
|
q3 := q2 / 1000
|
||||||
if q3 == 0 {
|
if q3 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||||
} else {
|
} else {
|
||||||
r3 := q2 - q3*1000
|
r3 := q2 - q3*1000
|
||||||
stream.buf[n] = byte(q3 + '0')
|
stream.buf[n] = byte(q3 + '0')
|
||||||
n++
|
n++
|
||||||
writeBuf(stream.buf, DIGITS[r3], n)
|
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||||
n += 3
|
n += 3
|
||||||
}
|
}
|
||||||
writeBuf(stream.buf, DIGITS[r2], n)
|
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+3)
|
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||||
stream.n = n + 6
|
stream.n = n + 6
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,62 +169,62 @@ func (stream *Stream) WriteUint64(val uint64) {
|
|||||||
n := stream.n
|
n := stream.n
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1*1000
|
r1 := val - q1*1000
|
||||||
q2 := q1 / 1000
|
q2 := q1 / 1000
|
||||||
if q2 == 0 {
|
if q2 == 0 {
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r2 := q1 - q2*1000
|
r2 := q1 - q2*1000
|
||||||
q3 := q2 / 1000
|
q3 := q2 / 1000
|
||||||
if q3 == 0 {
|
if q3 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n)
|
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+3)
|
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||||
stream.n = n + 6
|
stream.n = n + 6
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r3 := q2 - q3*1000
|
r3 := q2 - q3*1000
|
||||||
q4 := q3 / 1000
|
q4 := q3 / 1000
|
||||||
if q4 == 0 {
|
if q4 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q3], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q3], n)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n)
|
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n+3)
|
writeBuf(stream.buf, _DIGITS[r2], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+6)
|
writeBuf(stream.buf, _DIGITS[r1], n+6)
|
||||||
stream.n = n + 9
|
stream.n = n + 9
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r4 := q3 - q4*1000
|
r4 := q3 - q4*1000
|
||||||
q5 := q4 / 1000
|
q5 := q4 / 1000
|
||||||
if q5 == 0 {
|
if q5 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q4], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q4], n)
|
||||||
writeBuf(stream.buf, DIGITS[r4], n)
|
writeBuf(stream.buf, _DIGITS[r4], n)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n+3)
|
writeBuf(stream.buf, _DIGITS[r3], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n+6)
|
writeBuf(stream.buf, _DIGITS[r2], n+6)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+9)
|
writeBuf(stream.buf, _DIGITS[r1], n+9)
|
||||||
stream.n = n + 12
|
stream.n = n + 12
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r5 := q4 - q5*1000
|
r5 := q4 - q5*1000
|
||||||
q6 := q5 / 1000
|
q6 := q5 / 1000
|
||||||
if q6 == 0 {
|
if q6 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q5], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q5], n)
|
||||||
} else {
|
} else {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q6], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q6], n)
|
||||||
r6 := q5 - q6*1000
|
r6 := q5 - q6*1000
|
||||||
writeBuf(stream.buf, DIGITS[r6], n)
|
writeBuf(stream.buf, _DIGITS[r6], n)
|
||||||
n += 3
|
n += 3
|
||||||
}
|
}
|
||||||
writeBuf(stream.buf, DIGITS[r5], n)
|
writeBuf(stream.buf, _DIGITS[r5], n)
|
||||||
writeBuf(stream.buf, DIGITS[r4], n+3)
|
writeBuf(stream.buf, _DIGITS[r4], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n+6)
|
writeBuf(stream.buf, _DIGITS[r3], n+6)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n+9)
|
writeBuf(stream.buf, _DIGITS[r2], n+9)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+12)
|
writeBuf(stream.buf, _DIGITS[r1], n+12)
|
||||||
stream.n = n + 15
|
stream.n = n + 15
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,63 +241,63 @@ func (stream *Stream) WriteInt64(nval int64) {
|
|||||||
}
|
}
|
||||||
q1 := val / 1000
|
q1 := val / 1000
|
||||||
if q1 == 0 {
|
if q1 == 0 {
|
||||||
stream.n = writeFirstBuf(stream.buf, DIGITS[val], n)
|
stream.n = writeFirstBuf(stream.buf, _DIGITS[val], n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r1 := val - q1*1000
|
r1 := val - q1*1000
|
||||||
q2 := q1 / 1000
|
q2 := q1 / 1000
|
||||||
if q2 == 0 {
|
if q2 == 0 {
|
||||||
n := writeFirstBuf(stream.buf, DIGITS[q1], n)
|
n := writeFirstBuf(stream.buf, _DIGITS[q1], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n)
|
writeBuf(stream.buf, _DIGITS[r1], n)
|
||||||
stream.n = n + 3
|
stream.n = n + 3
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r2 := q1 - q2*1000
|
r2 := q1 - q2*1000
|
||||||
q3 := q2 / 1000
|
q3 := q2 / 1000
|
||||||
if q3 == 0 {
|
if q3 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q2], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n)
|
writeBuf(stream.buf, _DIGITS[r2], n)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+3)
|
writeBuf(stream.buf, _DIGITS[r1], n+3)
|
||||||
stream.n = n + 6
|
stream.n = n + 6
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r3 := q2 - q3*1000
|
r3 := q2 - q3*1000
|
||||||
q4 := q3 / 1000
|
q4 := q3 / 1000
|
||||||
if q4 == 0 {
|
if q4 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q3], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q3], n)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n)
|
writeBuf(stream.buf, _DIGITS[r3], n)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n+3)
|
writeBuf(stream.buf, _DIGITS[r2], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+6)
|
writeBuf(stream.buf, _DIGITS[r1], n+6)
|
||||||
stream.n = n + 9
|
stream.n = n + 9
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r4 := q3 - q4*1000
|
r4 := q3 - q4*1000
|
||||||
q5 := q4 / 1000
|
q5 := q4 / 1000
|
||||||
if q5 == 0 {
|
if q5 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q4], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q4], n)
|
||||||
writeBuf(stream.buf, DIGITS[r4], n)
|
writeBuf(stream.buf, _DIGITS[r4], n)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n+3)
|
writeBuf(stream.buf, _DIGITS[r3], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n+6)
|
writeBuf(stream.buf, _DIGITS[r2], n+6)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+9)
|
writeBuf(stream.buf, _DIGITS[r1], n+9)
|
||||||
stream.n = n + 12
|
stream.n = n + 12
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r5 := q4 - q5*1000
|
r5 := q4 - q5*1000
|
||||||
q6 := q5 / 1000
|
q6 := q5 / 1000
|
||||||
if q6 == 0 {
|
if q6 == 0 {
|
||||||
n = writeFirstBuf(stream.buf, DIGITS[q5], n)
|
n = writeFirstBuf(stream.buf, _DIGITS[q5], n)
|
||||||
} else {
|
} else {
|
||||||
stream.buf[n] = byte(q6 + '0')
|
stream.buf[n] = byte(q6 + '0')
|
||||||
n++
|
n++
|
||||||
r6 := q5 - q6*1000
|
r6 := q5 - q6*1000
|
||||||
writeBuf(stream.buf, DIGITS[r6], n)
|
writeBuf(stream.buf, _DIGITS[r6], n)
|
||||||
n += 3
|
n += 3
|
||||||
}
|
}
|
||||||
writeBuf(stream.buf, DIGITS[r5], n)
|
writeBuf(stream.buf, _DIGITS[r5], n)
|
||||||
writeBuf(stream.buf, DIGITS[r4], n+3)
|
writeBuf(stream.buf, _DIGITS[r4], n+3)
|
||||||
writeBuf(stream.buf, DIGITS[r3], n+6)
|
writeBuf(stream.buf, _DIGITS[r3], n+6)
|
||||||
writeBuf(stream.buf, DIGITS[r2], n+9)
|
writeBuf(stream.buf, _DIGITS[r2], n+9)
|
||||||
writeBuf(stream.buf, DIGITS[r1], n+12)
|
writeBuf(stream.buf, _DIGITS[r1], n+12)
|
||||||
stream.n = n + 15
|
stream.n = n + 15
|
||||||
}
|
}
|
||||||
|
|
||||||
|
351
feature_stream_string.go
Normal file
351
feature_stream_string.go
Normal file
@ -0,0 +1,351 @@
|
|||||||
|
package jsoniter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
// htmlSafeSet holds the value true if the ASCII character with the given
|
||||||
|
// array position can be safely represented inside a JSON string, embedded
|
||||||
|
// inside of HTML <script> tags, without any additional escaping.
|
||||||
|
//
|
||||||
|
// All values are true except for the ASCII control characters (0-31), the
|
||||||
|
// double quote ("), the backslash character ("\"), HTML opening and closing
|
||||||
|
// tags ("<" and ">"), and the ampersand ("&").
|
||||||
|
var htmlSafeSet = [utf8.RuneSelf]bool{
|
||||||
|
' ': true,
|
||||||
|
'!': true,
|
||||||
|
'"': false,
|
||||||
|
'#': true,
|
||||||
|
'$': true,
|
||||||
|
'%': true,
|
||||||
|
'&': false,
|
||||||
|
'\'': true,
|
||||||
|
'(': true,
|
||||||
|
')': true,
|
||||||
|
'*': true,
|
||||||
|
'+': true,
|
||||||
|
',': true,
|
||||||
|
'-': true,
|
||||||
|
'.': true,
|
||||||
|
'/': true,
|
||||||
|
'0': true,
|
||||||
|
'1': true,
|
||||||
|
'2': true,
|
||||||
|
'3': true,
|
||||||
|
'4': true,
|
||||||
|
'5': true,
|
||||||
|
'6': true,
|
||||||
|
'7': true,
|
||||||
|
'8': true,
|
||||||
|
'9': true,
|
||||||
|
':': true,
|
||||||
|
';': true,
|
||||||
|
'<': false,
|
||||||
|
'=': true,
|
||||||
|
'>': false,
|
||||||
|
'?': true,
|
||||||
|
'@': true,
|
||||||
|
'A': true,
|
||||||
|
'B': true,
|
||||||
|
'C': true,
|
||||||
|
'D': true,
|
||||||
|
'E': true,
|
||||||
|
'F': true,
|
||||||
|
'G': true,
|
||||||
|
'H': true,
|
||||||
|
'I': true,
|
||||||
|
'J': true,
|
||||||
|
'K': true,
|
||||||
|
'L': true,
|
||||||
|
'M': true,
|
||||||
|
'N': true,
|
||||||
|
'O': true,
|
||||||
|
'P': true,
|
||||||
|
'Q': true,
|
||||||
|
'R': true,
|
||||||
|
'S': true,
|
||||||
|
'T': true,
|
||||||
|
'U': true,
|
||||||
|
'V': true,
|
||||||
|
'W': true,
|
||||||
|
'X': true,
|
||||||
|
'Y': true,
|
||||||
|
'Z': true,
|
||||||
|
'[': true,
|
||||||
|
'\\': false,
|
||||||
|
']': true,
|
||||||
|
'^': true,
|
||||||
|
'_': true,
|
||||||
|
'`': true,
|
||||||
|
'a': true,
|
||||||
|
'b': true,
|
||||||
|
'c': true,
|
||||||
|
'd': true,
|
||||||
|
'e': true,
|
||||||
|
'f': true,
|
||||||
|
'g': true,
|
||||||
|
'h': true,
|
||||||
|
'i': true,
|
||||||
|
'j': true,
|
||||||
|
'k': true,
|
||||||
|
'l': true,
|
||||||
|
'm': true,
|
||||||
|
'n': true,
|
||||||
|
'o': true,
|
||||||
|
'p': true,
|
||||||
|
'q': true,
|
||||||
|
'r': true,
|
||||||
|
's': true,
|
||||||
|
't': true,
|
||||||
|
'u': true,
|
||||||
|
'v': true,
|
||||||
|
'w': true,
|
||||||
|
'x': true,
|
||||||
|
'y': true,
|
||||||
|
'z': true,
|
||||||
|
'{': true,
|
||||||
|
'|': true,
|
||||||
|
'}': true,
|
||||||
|
'~': true,
|
||||||
|
'\u007f': true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// safeSet holds the value true if the ASCII character with the given array
|
||||||
|
// position can be represented inside a JSON string without any further
|
||||||
|
// escaping.
|
||||||
|
//
|
||||||
|
// All values are true except for the ASCII control characters (0-31), the
|
||||||
|
// double quote ("), and the backslash character ("\").
|
||||||
|
var safeSet = [utf8.RuneSelf]bool{
|
||||||
|
' ': true,
|
||||||
|
'!': true,
|
||||||
|
'"': false,
|
||||||
|
'#': true,
|
||||||
|
'$': true,
|
||||||
|
'%': true,
|
||||||
|
'&': true,
|
||||||
|
'\'': true,
|
||||||
|
'(': true,
|
||||||
|
')': true,
|
||||||
|
'*': true,
|
||||||
|
'+': true,
|
||||||
|
',': true,
|
||||||
|
'-': true,
|
||||||
|
'.': true,
|
||||||
|
'/': true,
|
||||||
|
'0': true,
|
||||||
|
'1': true,
|
||||||
|
'2': true,
|
||||||
|
'3': true,
|
||||||
|
'4': true,
|
||||||
|
'5': true,
|
||||||
|
'6': true,
|
||||||
|
'7': true,
|
||||||
|
'8': true,
|
||||||
|
'9': true,
|
||||||
|
':': true,
|
||||||
|
';': true,
|
||||||
|
'<': true,
|
||||||
|
'=': true,
|
||||||
|
'>': true,
|
||||||
|
'?': true,
|
||||||
|
'@': true,
|
||||||
|
'A': true,
|
||||||
|
'B': true,
|
||||||
|
'C': true,
|
||||||
|
'D': true,
|
||||||
|
'E': true,
|
||||||
|
'F': true,
|
||||||
|
'G': true,
|
||||||
|
'H': true,
|
||||||
|
'I': true,
|
||||||
|
'J': true,
|
||||||
|
'K': true,
|
||||||
|
'L': true,
|
||||||
|
'M': true,
|
||||||
|
'N': true,
|
||||||
|
'O': true,
|
||||||
|
'P': true,
|
||||||
|
'Q': true,
|
||||||
|
'R': true,
|
||||||
|
'S': true,
|
||||||
|
'T': true,
|
||||||
|
'U': true,
|
||||||
|
'V': true,
|
||||||
|
'W': true,
|
||||||
|
'X': true,
|
||||||
|
'Y': true,
|
||||||
|
'Z': true,
|
||||||
|
'[': true,
|
||||||
|
'\\': false,
|
||||||
|
']': true,
|
||||||
|
'^': true,
|
||||||
|
'_': true,
|
||||||
|
'`': true,
|
||||||
|
'a': true,
|
||||||
|
'b': true,
|
||||||
|
'c': true,
|
||||||
|
'd': true,
|
||||||
|
'e': true,
|
||||||
|
'f': true,
|
||||||
|
'g': true,
|
||||||
|
'h': true,
|
||||||
|
'i': true,
|
||||||
|
'j': true,
|
||||||
|
'k': true,
|
||||||
|
'l': true,
|
||||||
|
'm': true,
|
||||||
|
'n': true,
|
||||||
|
'o': true,
|
||||||
|
'p': true,
|
||||||
|
'q': true,
|
||||||
|
'r': true,
|
||||||
|
's': true,
|
||||||
|
't': true,
|
||||||
|
'u': true,
|
||||||
|
'v': true,
|
||||||
|
'w': true,
|
||||||
|
'x': true,
|
||||||
|
'y': true,
|
||||||
|
'z': true,
|
||||||
|
'{': true,
|
||||||
|
'|': true,
|
||||||
|
'}': true,
|
||||||
|
'~': true,
|
||||||
|
'\u007f': true,
|
||||||
|
}
|
||||||
|
|
||||||
|
var hex = "0123456789abcdef"
|
||||||
|
|
||||||
|
func (stream *Stream) WriteStringWithHtmlEscaped(s string) {
|
||||||
|
stream.ensure(32)
|
||||||
|
valLen := len(s)
|
||||||
|
toWriteLen := valLen
|
||||||
|
bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
|
||||||
|
if stream.n+toWriteLen > bufLengthMinusTwo {
|
||||||
|
toWriteLen = bufLengthMinusTwo - stream.n
|
||||||
|
}
|
||||||
|
n := stream.n
|
||||||
|
stream.buf[n] = '"'
|
||||||
|
n++
|
||||||
|
// write string, the fast path, without utf8 and escape support
|
||||||
|
i := 0
|
||||||
|
for ; i < toWriteLen; i++ {
|
||||||
|
c := s[i]
|
||||||
|
if c <= utf8.RuneSelf && htmlSafeSet[c] {
|
||||||
|
stream.buf[n] = c
|
||||||
|
n++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i == valLen {
|
||||||
|
stream.buf[n] = '"'
|
||||||
|
n++
|
||||||
|
stream.n = n
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream.n = n
|
||||||
|
writeStringSlowPath(stream, htmlSafeSet, i, s, valLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stream *Stream) WriteString(s string) {
|
||||||
|
stream.ensure(32)
|
||||||
|
valLen := len(s)
|
||||||
|
toWriteLen := valLen
|
||||||
|
bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
|
||||||
|
if stream.n+toWriteLen > bufLengthMinusTwo {
|
||||||
|
toWriteLen = bufLengthMinusTwo - stream.n
|
||||||
|
}
|
||||||
|
n := stream.n
|
||||||
|
stream.buf[n] = '"'
|
||||||
|
n++
|
||||||
|
// write string, the fast path, without utf8 and escape support
|
||||||
|
i := 0
|
||||||
|
for ; i < toWriteLen; i++ {
|
||||||
|
c := s[i]
|
||||||
|
if c > 31 && c != '"' && c != '\\' {
|
||||||
|
stream.buf[n] = c
|
||||||
|
n++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if i == valLen {
|
||||||
|
stream.buf[n] = '"'
|
||||||
|
n++
|
||||||
|
stream.n = n
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stream.n = n
|
||||||
|
writeStringSlowPath(stream, safeSet, i, s, valLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeStringSlowPath(stream *Stream, safeSet [utf8.RuneSelf]bool, i int, s string, valLen int) {
|
||||||
|
start := i
|
||||||
|
// for the remaining parts, we process them char by char
|
||||||
|
for ; i < valLen; i++ {
|
||||||
|
if b := s[i]; b < utf8.RuneSelf {
|
||||||
|
if safeSet[b] {
|
||||||
|
i++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if start < i {
|
||||||
|
stream.WriteRaw(s[start:i])
|
||||||
|
}
|
||||||
|
switch b {
|
||||||
|
case '\\', '"':
|
||||||
|
stream.writeTwoBytes('\\', b)
|
||||||
|
case '\n':
|
||||||
|
stream.writeTwoBytes('\\', 'n')
|
||||||
|
case '\r':
|
||||||
|
stream.writeTwoBytes('\\', 'r')
|
||||||
|
case '\t':
|
||||||
|
stream.writeTwoBytes('\\', 't')
|
||||||
|
default:
|
||||||
|
// This encodes bytes < 0x20 except for \t, \n and \r.
|
||||||
|
// If escapeHTML is set, it also escapes <, >, and &
|
||||||
|
// because they can lead to security holes when
|
||||||
|
// user-controlled strings are rendered into JSON
|
||||||
|
// and served to some browsers.
|
||||||
|
stream.WriteRaw(`\u00`)
|
||||||
|
stream.writeTwoBytes(hex[b>>4], hex[b&0xF])
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
start = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c, size := utf8.DecodeRuneInString(s[i:])
|
||||||
|
if c == utf8.RuneError && size == 1 {
|
||||||
|
if start < i {
|
||||||
|
stream.WriteRaw(s[start:i])
|
||||||
|
}
|
||||||
|
stream.WriteRaw(`\ufffd`)
|
||||||
|
i += size
|
||||||
|
start = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// U+2028 is LINE SEPARATOR.
|
||||||
|
// U+2029 is PARAGRAPH SEPARATOR.
|
||||||
|
// They are both technically valid characters in JSON strings,
|
||||||
|
// but don't work in JSONP, which has to be evaluated as JavaScript,
|
||||||
|
// and can lead to security holes there. It is valid JSON to
|
||||||
|
// escape them, so we do so unconditionally.
|
||||||
|
// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
|
||||||
|
if c == '\u2028' || c == '\u2029' {
|
||||||
|
if start < i {
|
||||||
|
stream.WriteRaw(s[start:i])
|
||||||
|
}
|
||||||
|
stream.WriteRaw(`\u202`)
|
||||||
|
stream.writeByte(hex[c&0xF])
|
||||||
|
i += size
|
||||||
|
start = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i += size
|
||||||
|
}
|
||||||
|
if start < len(s) {
|
||||||
|
stream.WriteRaw(s[start:])
|
||||||
|
}
|
||||||
|
stream.writeByte('"')
|
||||||
|
}
|
@ -37,10 +37,12 @@ func Test_new_encoder(t *testing.T) {
|
|||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf1 := &bytes.Buffer{}
|
buf1 := &bytes.Buffer{}
|
||||||
encoder1 := json.NewEncoder(buf1)
|
encoder1 := json.NewEncoder(buf1)
|
||||||
|
encoder1.SetEscapeHTML(false)
|
||||||
encoder1.Encode([]int{1})
|
encoder1.Encode([]int{1})
|
||||||
should.Equal("[1]\n", buf1.String())
|
should.Equal("[1]\n", buf1.String())
|
||||||
buf2 := &bytes.Buffer{}
|
buf2 := &bytes.Buffer{}
|
||||||
encoder2 := NewEncoder(buf2)
|
encoder2 := NewEncoder(buf2)
|
||||||
|
encoder2.SetEscapeHTML(false)
|
||||||
encoder2.Encode([]int{1})
|
encoder2.Encode([]int{1})
|
||||||
should.Equal("[1]", buf2.String())
|
should.Equal("[1]", buf2.String())
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,10 @@ import (
|
|||||||
|
|
||||||
func Test_empty_array(t *testing.T) {
|
func Test_empty_array(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`[]`)
|
iter := ParseString(ConfigOfDefault, `[]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
should.False(cont)
|
should.False(cont)
|
||||||
iter = ParseString(`[]`)
|
iter = ParseString(ConfigOfDefault, `[]`)
|
||||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||||
should.FailNow("should not call")
|
should.FailNow("should not call")
|
||||||
return true
|
return true
|
||||||
@ -22,11 +22,11 @@ func Test_empty_array(t *testing.T) {
|
|||||||
|
|
||||||
func Test_one_element(t *testing.T) {
|
func Test_one_element(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`[1]`)
|
iter := ParseString(ConfigOfDefault, `[1]`)
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.Equal(1, iter.ReadInt())
|
should.Equal(1, iter.ReadInt())
|
||||||
should.False(iter.ReadArray())
|
should.False(iter.ReadArray())
|
||||||
iter = ParseString(`[1]`)
|
iter = ParseString(ConfigOfDefault, `[1]`)
|
||||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||||
should.Equal(1, iter.ReadInt())
|
should.Equal(1, iter.ReadInt())
|
||||||
return true
|
return true
|
||||||
@ -35,13 +35,13 @@ func Test_one_element(t *testing.T) {
|
|||||||
|
|
||||||
func Test_two_elements(t *testing.T) {
|
func Test_two_elements(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`[1,2]`)
|
iter := ParseString(ConfigOfDefault, `[1,2]`)
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.Equal(int64(1), iter.ReadInt64())
|
should.Equal(int64(1), iter.ReadInt64())
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.Equal(int64(2), iter.ReadInt64())
|
should.Equal(int64(2), iter.ReadInt64())
|
||||||
should.False(iter.ReadArray())
|
should.False(iter.ReadArray())
|
||||||
iter = ParseString(`[1,2]`)
|
iter = ParseString(ConfigOfDefault, `[1,2]`)
|
||||||
should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
|
should.Equal([]interface{}{float64(1), float64(2)}, iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +152,7 @@ func Test_invalid_array(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_whitespace_in_head(t *testing.T) {
|
func Test_whitespace_in_head(t *testing.T) {
|
||||||
iter := ParseString(` [1]`)
|
iter := ParseString(ConfigOfDefault, ` [1]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
if cont != true {
|
if cont != true {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -163,7 +163,7 @@ func Test_whitespace_in_head(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_whitespace_after_array_start(t *testing.T) {
|
func Test_whitespace_after_array_start(t *testing.T) {
|
||||||
iter := ParseString(`[ 1]`)
|
iter := ParseString(ConfigOfDefault, `[ 1]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
if cont != true {
|
if cont != true {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -174,7 +174,7 @@ func Test_whitespace_after_array_start(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_whitespace_before_array_end(t *testing.T) {
|
func Test_whitespace_before_array_end(t *testing.T) {
|
||||||
iter := ParseString(`[1 ]`)
|
iter := ParseString(ConfigOfDefault, `[1 ]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
if cont != true {
|
if cont != true {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -189,7 +189,7 @@ func Test_whitespace_before_array_end(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_whitespace_before_comma(t *testing.T) {
|
func Test_whitespace_before_comma(t *testing.T) {
|
||||||
iter := ParseString(`[1 ,2]`)
|
iter := ParseString(ConfigOfDefault, `[1 ,2]`)
|
||||||
cont := iter.ReadArray()
|
cont := iter.ReadArray()
|
||||||
if cont != true {
|
if cont != true {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -213,8 +213,7 @@ func Test_whitespace_before_comma(t *testing.T) {
|
|||||||
func Test_write_array(t *testing.T) {
|
func Test_write_array(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
|
||||||
stream.IndentionStep = 2
|
|
||||||
stream.WriteArrayStart()
|
stream.WriteArrayStart()
|
||||||
stream.WriteInt(1)
|
stream.WriteInt(1)
|
||||||
stream.WriteMore()
|
stream.WriteMore()
|
||||||
@ -228,7 +227,7 @@ func Test_write_array(t *testing.T) {
|
|||||||
func Test_write_val_array(t *testing.T) {
|
func Test_write_val_array(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
val := []int{1, 2, 3}
|
val := []int{1, 2, 3}
|
||||||
str, err := MarshalToString(val)
|
str, err := MarshalToString(&val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal("[1,2,3]", str)
|
should.Equal("[1,2,3]", str)
|
||||||
}
|
}
|
||||||
@ -288,7 +287,7 @@ func Test_decode_byte_array(t *testing.T) {
|
|||||||
func Benchmark_jsoniter_array(b *testing.B) {
|
func Benchmark_jsoniter_array(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
input := []byte(`[1,2,3,4,5,6,7,8,9]`)
|
input := []byte(`[1,2,3,4,5,6,7,8,9]`)
|
||||||
iter := ParseBytes(input)
|
iter := ParseBytes(ConfigOfDefault, input)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter.ResetBytes(input)
|
iter.ResetBytes(input)
|
||||||
|
File diff suppressed because one or more lines are too long
@ -8,15 +8,15 @@ import (
|
|||||||
|
|
||||||
func Test_true(t *testing.T) {
|
func Test_true(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`true`)
|
iter := ParseString(ConfigOfDefault, `true`)
|
||||||
should.True(iter.ReadBool())
|
should.True(iter.ReadBool())
|
||||||
iter = ParseString(`true`)
|
iter = ParseString(ConfigOfDefault, `true`)
|
||||||
should.Equal(true, iter.Read())
|
should.Equal(true, iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_false(t *testing.T) {
|
func Test_false(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`false`)
|
iter := ParseString(ConfigOfDefault, `false`)
|
||||||
should.False(iter.ReadBool())
|
should.False(iter.ReadBool())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ func Test_read_bool_as_any(t *testing.T) {
|
|||||||
func Test_write_true_false(t *testing.T) {
|
func Test_write_true_false(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteTrue()
|
stream.WriteTrue()
|
||||||
stream.WriteFalse()
|
stream.WriteFalse()
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -41,7 +41,7 @@ func Test_write_true_false(t *testing.T) {
|
|||||||
func Test_write_val_bool(t *testing.T) {
|
func Test_write_val_bool(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(true)
|
stream.WriteVal(true)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
|
@ -19,7 +19,7 @@ func Test_customize_type_decoder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
*((*time.Time)(ptr)) = t
|
*((*time.Time)(ptr)) = t
|
||||||
})
|
})
|
||||||
defer CleanDecoders()
|
defer ConfigOfDefault.CleanDecoders()
|
||||||
val := time.Time{}
|
val := time.Time{}
|
||||||
err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
|
err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -37,7 +37,7 @@ func Test_customize_type_encoder(t *testing.T) {
|
|||||||
t := *((*time.Time)(ptr))
|
t := *((*time.Time)(ptr))
|
||||||
stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
|
stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
|
||||||
})
|
})
|
||||||
defer CleanEncoders()
|
defer ConfigOfDefault.CleanEncoders()
|
||||||
val := time.Unix(0, 0)
|
val := time.Unix(0, 0)
|
||||||
str, err := MarshalToString(val)
|
str, err := MarshalToString(val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
@ -45,13 +45,13 @@ func Test_customize_type_encoder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_customize_byte_array_encoder(t *testing.T) {
|
func Test_customize_byte_array_encoder(t *testing.T) {
|
||||||
CleanEncoders()
|
ConfigOfDefault.CleanEncoders()
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
|
RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
|
||||||
t := *((*[]byte)(ptr))
|
t := *((*[]byte)(ptr))
|
||||||
stream.WriteString(string(t))
|
stream.WriteString(string(t))
|
||||||
})
|
})
|
||||||
defer CleanEncoders()
|
defer ConfigOfDefault.CleanEncoders()
|
||||||
val := []byte("abc")
|
val := []byte("abc")
|
||||||
str, err := MarshalToString(val)
|
str, err := MarshalToString(val)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
@ -60,9 +60,8 @@ func Test_customize_byte_array_encoder(t *testing.T) {
|
|||||||
|
|
||||||
func Test_customize_float_marshal(t *testing.T) {
|
func Test_customize_float_marshal(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
EnableLossyFloatMarshalling()
|
json := Config{MarshalFloatWith6Digits: true}.Froze()
|
||||||
defer CleanEncoders()
|
str, err := json.MarshalToString(float32(1.23456789))
|
||||||
str, err := MarshalToString(float32(1.23456789))
|
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal("1.234568", str)
|
should.Equal("1.234568", str)
|
||||||
}
|
}
|
||||||
@ -75,7 +74,7 @@ func Test_customize_field_decoder(t *testing.T) {
|
|||||||
RegisterFieldDecoder("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
|
RegisterFieldDecoder("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||||
})
|
})
|
||||||
defer CleanDecoders()
|
defer ConfigOfDefault.CleanDecoders()
|
||||||
tom := Tom{}
|
tom := Tom{}
|
||||||
err := Unmarshal([]byte(`{"field1": 100}`), &tom)
|
err := Unmarshal([]byte(`{"field1": 100}`), &tom)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -113,7 +112,7 @@ func Test_customize_field_by_extension(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_unexported_fields(t *testing.T) {
|
func Test_unexported_fields(t *testing.T) {
|
||||||
EnableUnexportedStructFieldsSupport()
|
jsoniter := Config{SupportUnexportedStructFields: true}.Froze()
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
field1 string
|
field1 string
|
||||||
@ -121,12 +120,12 @@ func Test_unexported_fields(t *testing.T) {
|
|||||||
}
|
}
|
||||||
obj := TestObject{}
|
obj := TestObject{}
|
||||||
obj.field1 = "hello"
|
obj.field1 = "hello"
|
||||||
should.Nil(UnmarshalFromString(`{}`, &obj))
|
should.Nil(jsoniter.UnmarshalFromString(`{}`, &obj))
|
||||||
should.Equal("hello", obj.field1)
|
should.Equal("hello", obj.field1)
|
||||||
should.Nil(UnmarshalFromString(`{"field1": "world", "field-2": "abc"}`, &obj))
|
should.Nil(jsoniter.UnmarshalFromString(`{"field1": "world", "field-2": "abc"}`, &obj))
|
||||||
should.Equal("world", obj.field1)
|
should.Equal("world", obj.field1)
|
||||||
should.Equal("abc", obj.field2)
|
should.Equal("abc", obj.field2)
|
||||||
str, err := MarshalToString(obj)
|
str, err := jsoniter.MarshalToString(obj)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Contains(str, `"field-2":"abc"`)
|
should.Contains(str, `"field-2":"abc"`)
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ func Test_bind_api_demo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_iterator_api_demo(t *testing.T) {
|
func Test_iterator_api_demo(t *testing.T) {
|
||||||
iter := ParseString(`[0,1,2,3]`)
|
iter := ParseString(ConfigOfDefault, `[0,1,2,3]`)
|
||||||
total := 0
|
total := 0
|
||||||
for iter.ReadArray() {
|
for iter.ReadArray() {
|
||||||
total += iter.ReadInt()
|
total += iter.ReadInt()
|
||||||
|
@ -7,56 +7,56 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_string_end(t *testing.T) {
|
func Test_string_end(t *testing.T) {
|
||||||
end, escaped := ParseString(`abc"`).findStringEnd()
|
end, escaped := ParseString(ConfigOfDefault, `abc"`).findStringEnd()
|
||||||
if end != 4 {
|
if end != 4 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != false {
|
if escaped != false {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\\"`).findStringEnd()
|
end, escaped = ParseString(ConfigOfDefault, `abc\\"`).findStringEnd()
|
||||||
if end != 6 {
|
if end != 6 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != true {
|
if escaped != true {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\\\\"`).findStringEnd()
|
end, escaped = ParseString(ConfigOfDefault, `abc\\\\"`).findStringEnd()
|
||||||
if end != 8 {
|
if end != 8 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != true {
|
if escaped != true {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\"`).findStringEnd()
|
end, escaped = ParseString(ConfigOfDefault, `abc\"`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != false {
|
if escaped != false {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\`).findStringEnd()
|
end, escaped = ParseString(ConfigOfDefault, `abc\`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != true {
|
if escaped != true {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`abc\\`).findStringEnd()
|
end, escaped = ParseString(ConfigOfDefault, `abc\\`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != false {
|
if escaped != false {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`\\`).findStringEnd()
|
end, escaped = ParseString(ConfigOfDefault, `\\`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
if escaped != false {
|
if escaped != false {
|
||||||
t.Fatal(escaped)
|
t.Fatal(escaped)
|
||||||
}
|
}
|
||||||
end, escaped = ParseString(`\`).findStringEnd()
|
end, escaped = ParseString(ConfigOfDefault, `\`).findStringEnd()
|
||||||
if end != -1 {
|
if end != -1 {
|
||||||
t.Fatal(end)
|
t.Fatal(end)
|
||||||
}
|
}
|
||||||
@ -91,54 +91,54 @@ func (reader *StagedReader) Read(p []byte) (n int, err error) {
|
|||||||
|
|
||||||
func Test_skip_string(t *testing.T) {
|
func Test_skip_string(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`"abc`)
|
iter := ParseString(ConfigOfDefault, `"abc`)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(1, iter.head)
|
should.Equal(1, iter.head)
|
||||||
iter = ParseString(`\""abc`)
|
iter = ParseString(ConfigOfDefault, `\""abc`)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(3, iter.head)
|
should.Equal(3, iter.head)
|
||||||
reader := &StagedReader{
|
reader := &StagedReader{
|
||||||
r1: `abc`,
|
r1: `abc`,
|
||||||
r2: `"`,
|
r2: `"`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigOfDefault, reader, 4096)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(1, iter.head)
|
should.Equal(1, iter.head)
|
||||||
reader = &StagedReader{
|
reader = &StagedReader{
|
||||||
r1: `abc`,
|
r1: `abc`,
|
||||||
r2: `1"`,
|
r2: `1"`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigOfDefault, reader, 4096)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(2, iter.head)
|
should.Equal(2, iter.head)
|
||||||
reader = &StagedReader{
|
reader = &StagedReader{
|
||||||
r1: `abc\`,
|
r1: `abc\`,
|
||||||
r2: `"`,
|
r2: `"`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigOfDefault, reader, 4096)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
reader = &StagedReader{
|
reader = &StagedReader{
|
||||||
r1: `abc\`,
|
r1: `abc\`,
|
||||||
r2: `""`,
|
r2: `""`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigOfDefault, reader, 4096)
|
||||||
iter.skipString()
|
iter.skipString()
|
||||||
should.Equal(2, iter.head)
|
should.Equal(2, iter.head)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_object(t *testing.T) {
|
func Test_skip_object(t *testing.T) {
|
||||||
iter := ParseString(`}`)
|
iter := ParseString(ConfigOfDefault, `}`)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 1 {
|
if iter.head != 1 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
}
|
}
|
||||||
iter = ParseString(`a}`)
|
iter = ParseString(ConfigOfDefault, `a}`)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 2 {
|
if iter.head != 2 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
}
|
}
|
||||||
iter = ParseString(`{}}a`)
|
iter = ParseString(ConfigOfDefault, `{}}a`)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 3 {
|
if iter.head != 3 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
@ -147,12 +147,12 @@ func Test_skip_object(t *testing.T) {
|
|||||||
r1: `{`,
|
r1: `{`,
|
||||||
r2: `}}a`,
|
r2: `}}a`,
|
||||||
}
|
}
|
||||||
iter = Parse(reader, 4096)
|
iter = Parse(ConfigOfDefault, reader, 4096)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 2 {
|
if iter.head != 2 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
}
|
}
|
||||||
iter = ParseString(`"}"}a`)
|
iter = ParseString(ConfigOfDefault, `"}"}a`)
|
||||||
iter.skipObject()
|
iter.skipObject()
|
||||||
if iter.head != 4 {
|
if iter.head != 4 {
|
||||||
t.Fatal(iter.head)
|
t.Fatal(iter.head)
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
func Test_read_big_float(t *testing.T) {
|
func Test_read_big_float(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`12.3`)
|
iter := ParseString(ConfigOfDefault, `12.3`)
|
||||||
val := iter.ReadBigFloat()
|
val := iter.ReadBigFloat()
|
||||||
val64, _ := val.Float64()
|
val64, _ := val.Float64()
|
||||||
should.Equal(12.3, val64)
|
should.Equal(12.3, val64)
|
||||||
@ -19,7 +19,7 @@ func Test_read_big_float(t *testing.T) {
|
|||||||
|
|
||||||
func Test_read_big_int(t *testing.T) {
|
func Test_read_big_int(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`92233720368547758079223372036854775807`)
|
iter := ParseString(ConfigOfDefault, `92233720368547758079223372036854775807`)
|
||||||
val := iter.ReadBigInt()
|
val := iter.ReadBigInt()
|
||||||
should.NotNil(val)
|
should.NotNil(val)
|
||||||
should.Equal(`92233720368547758079223372036854775807`, val.String())
|
should.Equal(`92233720368547758079223372036854775807`, val.String())
|
||||||
@ -31,14 +31,14 @@ func Test_read_float(t *testing.T) {
|
|||||||
// non-streaming
|
// non-streaming
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input + ",")
|
iter := ParseString(ConfigOfDefault, input+",")
|
||||||
expected, err := strconv.ParseFloat(input, 32)
|
expected, err := strconv.ParseFloat(input, 32)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(float32(expected), iter.ReadFloat32())
|
should.Equal(float32(expected), iter.ReadFloat32())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input + ",")
|
iter := ParseString(ConfigOfDefault, input+",")
|
||||||
expected, err := strconv.ParseFloat(input, 64)
|
expected, err := strconv.ParseFloat(input, 64)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(expected, iter.ReadFloat64())
|
should.Equal(expected, iter.ReadFloat64())
|
||||||
@ -46,14 +46,14 @@ func Test_read_float(t *testing.T) {
|
|||||||
// streaming
|
// streaming
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input+","), 2)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input+","), 2)
|
||||||
expected, err := strconv.ParseFloat(input, 32)
|
expected, err := strconv.ParseFloat(input, 32)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(float32(expected), iter.ReadFloat32())
|
should.Equal(float32(expected), iter.ReadFloat32())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input+","), 2)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input+","), 2)
|
||||||
expected, err := strconv.ParseFloat(input, 64)
|
expected, err := strconv.ParseFloat(input, 64)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(expected, iter.ReadFloat64())
|
should.Equal(expected, iter.ReadFloat64())
|
||||||
@ -63,7 +63,7 @@ func Test_read_float(t *testing.T) {
|
|||||||
|
|
||||||
func Test_read_float_as_interface(t *testing.T) {
|
func Test_read_float_as_interface(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`12.3`)
|
iter := ParseString(ConfigOfDefault, `12.3`)
|
||||||
should.Equal(float64(12.3), iter.Read())
|
should.Equal(float64(12.3), iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ func Test_write_float32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteFloat32Lossy(val)
|
stream.WriteFloat32Lossy(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -99,7 +99,7 @@ func Test_write_float32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -108,7 +108,7 @@ func Test_write_float32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigOfDefault, buf, 10)
|
||||||
stream.WriteRaw("abcdefg")
|
stream.WriteRaw("abcdefg")
|
||||||
stream.WriteFloat32Lossy(1.123456)
|
stream.WriteFloat32Lossy(1.123456)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -123,7 +123,7 @@ func Test_write_float64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteFloat64Lossy(val)
|
stream.WriteFloat64Lossy(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -132,7 +132,7 @@ func Test_write_float64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -141,7 +141,7 @@ func Test_write_float64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigOfDefault, buf, 10)
|
||||||
stream.WriteRaw("abcdefg")
|
stream.WriteRaw("abcdefg")
|
||||||
stream.WriteFloat64Lossy(1.123456)
|
stream.WriteFloat64Lossy(1.123456)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -151,7 +151,7 @@ func Test_write_float64(t *testing.T) {
|
|||||||
|
|
||||||
func Test_read_float64_cursor(t *testing.T) {
|
func Test_read_float64_cursor(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString("[1.23456789\n,2,3]")
|
iter := ParseString(ConfigOfDefault, "[1.23456789\n,2,3]")
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.Equal(1.23456789, iter.Read())
|
should.Equal(1.23456789, iter.Read())
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
@ -174,7 +174,7 @@ func Test_read_float_scientific(t *testing.T) {
|
|||||||
func Benchmark_jsoniter_float(b *testing.B) {
|
func Benchmark_jsoniter_float(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
input := []byte(`1.1123,`)
|
input := []byte(`1.1123,`)
|
||||||
iter := NewIterator()
|
iter := NewIterator(ConfigOfDefault)
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter.ResetBytes(input)
|
iter.ResetBytes(input)
|
||||||
iter.ReadFloat64()
|
iter.ReadFloat64()
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
func Test_read_uint64_invalid(t *testing.T) {
|
func Test_read_uint64_invalid(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(",")
|
iter := ParseString(ConfigOfDefault, ",")
|
||||||
iter.ReadUint64()
|
iter.ReadUint64()
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
}
|
}
|
||||||
@ -23,7 +23,7 @@ func Test_read_int8(t *testing.T) {
|
|||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigOfDefault, input)
|
||||||
expected, err := strconv.ParseInt(input, 10, 8)
|
expected, err := strconv.ParseInt(input, 10, 8)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(int8(expected), iter.ReadInt8())
|
should.Equal(int8(expected), iter.ReadInt8())
|
||||||
@ -36,7 +36,7 @@ func Test_read_int16(t *testing.T) {
|
|||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigOfDefault, input)
|
||||||
expected, err := strconv.ParseInt(input, 10, 16)
|
expected, err := strconv.ParseInt(input, 10, 16)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(int16(expected), iter.ReadInt16())
|
should.Equal(int16(expected), iter.ReadInt16())
|
||||||
@ -49,14 +49,14 @@ func Test_read_int32(t *testing.T) {
|
|||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigOfDefault, input)
|
||||||
expected, err := strconv.ParseInt(input, 10, 32)
|
expected, err := strconv.ParseInt(input, 10, 32)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(int32(expected), iter.ReadInt32())
|
should.Equal(int32(expected), iter.ReadInt32())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
|
||||||
expected, err := strconv.ParseInt(input, 10, 32)
|
expected, err := strconv.ParseInt(input, 10, 32)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(int32(expected), iter.ReadInt32())
|
should.Equal(int32(expected), iter.ReadInt32())
|
||||||
@ -83,7 +83,7 @@ func Test_read_int64_array(t *testing.T) {
|
|||||||
func Test_read_int32_overflow(t *testing.T) {
|
func Test_read_int32_overflow(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
input := "123456789123456789,"
|
input := "123456789123456789,"
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigOfDefault, input)
|
||||||
iter.ReadInt32()
|
iter.ReadInt32()
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
}
|
}
|
||||||
@ -93,14 +93,14 @@ func Test_read_int64(t *testing.T) {
|
|||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigOfDefault, input)
|
||||||
expected, err := strconv.ParseInt(input, 10, 64)
|
expected, err := strconv.ParseInt(input, 10, 64)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(expected, iter.ReadInt64())
|
should.Equal(expected, iter.ReadInt64())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
|
||||||
expected, err := strconv.ParseInt(input, 10, 64)
|
expected, err := strconv.ParseInt(input, 10, 64)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(expected, iter.ReadInt64())
|
should.Equal(expected, iter.ReadInt64())
|
||||||
@ -111,7 +111,7 @@ func Test_read_int64(t *testing.T) {
|
|||||||
func Test_read_int64_overflow(t *testing.T) {
|
func Test_read_int64_overflow(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
input := "123456789123456789123456789123456789,"
|
input := "123456789123456789123456789123456789,"
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigOfDefault, input)
|
||||||
iter.ReadInt64()
|
iter.ReadInt64()
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
}
|
}
|
||||||
@ -146,7 +146,7 @@ func Test_write_uint8(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteUint8(val)
|
stream.WriteUint8(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -155,7 +155,7 @@ func Test_write_uint8(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -164,7 +164,7 @@ func Test_write_uint8(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 3)
|
stream := NewStream(ConfigOfDefault, buf, 3)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteUint8(100) // should clear buffer
|
stream.WriteUint8(100) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -178,7 +178,7 @@ func Test_write_int8(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteInt8(val)
|
stream.WriteInt8(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -187,7 +187,7 @@ func Test_write_int8(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -196,7 +196,7 @@ func Test_write_int8(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4)
|
stream := NewStream(ConfigOfDefault, buf, 4)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteInt8(-100) // should clear buffer
|
stream.WriteInt8(-100) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -210,7 +210,7 @@ func Test_write_uint16(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteUint16(val)
|
stream.WriteUint16(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -219,7 +219,7 @@ func Test_write_uint16(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -228,7 +228,7 @@ func Test_write_uint16(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 5)
|
stream := NewStream(ConfigOfDefault, buf, 5)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteUint16(10000) // should clear buffer
|
stream.WriteUint16(10000) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -242,7 +242,7 @@ func Test_write_int16(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteInt16(val)
|
stream.WriteInt16(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -251,7 +251,7 @@ func Test_write_int16(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -260,7 +260,7 @@ func Test_write_int16(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 6)
|
stream := NewStream(ConfigOfDefault, buf, 6)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteInt16(-10000) // should clear buffer
|
stream.WriteInt16(-10000) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -274,7 +274,7 @@ func Test_write_uint32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteUint32(val)
|
stream.WriteUint32(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -283,7 +283,7 @@ func Test_write_uint32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -292,7 +292,7 @@ func Test_write_uint32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigOfDefault, buf, 10)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteUint32(0xffffffff) // should clear buffer
|
stream.WriteUint32(0xffffffff) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -306,7 +306,7 @@ func Test_write_int32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteInt32(val)
|
stream.WriteInt32(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -315,7 +315,7 @@ func Test_write_int32(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -324,7 +324,7 @@ func Test_write_int32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 11)
|
stream := NewStream(ConfigOfDefault, buf, 11)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteInt32(-0x7fffffff) // should clear buffer
|
stream.WriteInt32(-0x7fffffff) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -340,7 +340,7 @@ func Test_write_uint64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteUint64(val)
|
stream.WriteUint64(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -349,7 +349,7 @@ func Test_write_uint64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -358,7 +358,7 @@ func Test_write_uint64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigOfDefault, buf, 10)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteUint64(0xffffffff) // should clear buffer
|
stream.WriteUint64(0xffffffff) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -374,7 +374,7 @@ func Test_write_int64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteInt64(val)
|
stream.WriteInt64(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -383,7 +383,7 @@ func Test_write_int64(t *testing.T) {
|
|||||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(val)
|
stream.WriteVal(val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -392,7 +392,7 @@ func Test_write_int64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 10)
|
stream := NewStream(ConfigOfDefault, buf, 10)
|
||||||
stream.WriteRaw("a")
|
stream.WriteRaw("a")
|
||||||
stream.WriteInt64(0xffffffff) // should clear buffer
|
stream.WriteInt64(0xffffffff) // should clear buffer
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -403,7 +403,7 @@ func Test_write_int64(t *testing.T) {
|
|||||||
func Test_write_val_int(t *testing.T) {
|
func Test_write_val_int(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal(1001)
|
stream.WriteVal(1001)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -413,7 +413,7 @@ func Test_write_val_int(t *testing.T) {
|
|||||||
func Test_write_val_int_ptr(t *testing.T) {
|
func Test_write_val_int_ptr(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
val := 1001
|
val := 1001
|
||||||
stream.WriteVal(&val)
|
stream.WriteVal(&val)
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
@ -433,7 +433,7 @@ func Test_json_number(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_encode_int(b *testing.B) {
|
func Benchmark_jsoniter_encode_int(b *testing.B) {
|
||||||
stream := NewStream(ioutil.Discard, 64)
|
stream := NewStream(ConfigOfDefault, ioutil.Discard, 64)
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
stream.n = 0
|
stream.n = 0
|
||||||
stream.WriteUint64(0xffffffff)
|
stream.WriteUint64(0xffffffff)
|
||||||
@ -447,7 +447,7 @@ func Benchmark_itoa(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_int(b *testing.B) {
|
func Benchmark_jsoniter_int(b *testing.B) {
|
||||||
iter := NewIterator()
|
iter := NewIterator(ConfigOfDefault)
|
||||||
input := []byte(`100`)
|
input := []byte(`100`)
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter.ResetBytes(input)
|
iter.ResetBytes(input)
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
"testing"
|
"testing"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
"encoding/json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_write_array_of_interface(t *testing.T) {
|
func Test_write_array_of_interface(t *testing.T) {
|
||||||
@ -140,10 +140,9 @@ func Test_encode_object_contain_non_empty_interface(t *testing.T) {
|
|||||||
should.Equal(`{"Field":"hello"}`, str)
|
should.Equal(`{"Field":"hello"}`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func Test_nil_non_empty_interface(t *testing.T) {
|
func Test_nil_non_empty_interface(t *testing.T) {
|
||||||
CleanEncoders()
|
ConfigOfDefault.CleanEncoders()
|
||||||
CleanDecoders()
|
ConfigOfDefault.CleanDecoders()
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
Field []MyInterface
|
Field []MyInterface
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_by_one(t *testing.T) {
|
func Test_read_by_one(t *testing.T) {
|
||||||
iter := Parse(bytes.NewBufferString("abc"), 1)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString("abc"), 1)
|
||||||
b := iter.readByte()
|
b := iter.readByte()
|
||||||
if iter.Error != nil {
|
if iter.Error != nil {
|
||||||
t.Fatal(iter.Error)
|
t.Fatal(iter.Error)
|
||||||
@ -34,7 +34,7 @@ func Test_read_by_one(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_by_two(t *testing.T) {
|
func Test_read_by_two(t *testing.T) {
|
||||||
iter := Parse(bytes.NewBufferString("abc"), 2)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString("abc"), 2)
|
||||||
b := iter.readByte()
|
b := iter.readByte()
|
||||||
if iter.Error != nil {
|
if iter.Error != nil {
|
||||||
t.Fatal(iter.Error)
|
t.Fatal(iter.Error)
|
||||||
@ -67,7 +67,7 @@ func Test_read_by_two(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_read_until_eof(t *testing.T) {
|
func Test_read_until_eof(t *testing.T) {
|
||||||
iter := Parse(bytes.NewBufferString("abc"), 2)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString("abc"), 2)
|
||||||
iter.readByte()
|
iter.readByte()
|
||||||
iter.readByte()
|
iter.readByte()
|
||||||
b := iter.readByte()
|
b := iter.readByte()
|
||||||
|
@ -27,7 +27,7 @@ func Benchmark_jsoniter_large_file(b *testing.B) {
|
|||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
file, _ := os.Open("/tmp/large-file.json")
|
file, _ := os.Open("/tmp/large-file.json")
|
||||||
iter := Parse(file, 4096)
|
iter := Parse(ConfigOfDefault, file, 4096)
|
||||||
count := 0
|
count := 0
|
||||||
for iter.ReadArray() {
|
for iter.ReadArray() {
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
"encoding/json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_map(t *testing.T) {
|
func Test_read_map(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{"hello": "world"}`)
|
iter := ParseString(ConfigOfDefault, `{"hello": "world"}`)
|
||||||
m := map[string]string{"1": "2"}
|
m := map[string]string{"1": "2"}
|
||||||
iter.ReadVal(&m)
|
iter.ReadVal(&m)
|
||||||
copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
|
copy(iter.buf, []byte{0, 0, 0, 0, 0, 0})
|
||||||
@ -18,11 +18,11 @@ func Test_read_map(t *testing.T) {
|
|||||||
|
|
||||||
func Test_read_map_of_interface(t *testing.T) {
|
func Test_read_map_of_interface(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{"hello": "world"}`)
|
iter := ParseString(ConfigOfDefault, `{"hello": "world"}`)
|
||||||
m := map[string]interface{}{"1": "2"}
|
m := map[string]interface{}{"1": "2"}
|
||||||
iter.ReadVal(&m)
|
iter.ReadVal(&m)
|
||||||
should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
|
should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
|
||||||
iter = ParseString(`{"hello": "world"}`)
|
iter = ParseString(ConfigOfDefault, `{"hello": "world"}`)
|
||||||
should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
|
should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,8 +103,6 @@ func Test_decode_TextMarshaler_key_map(t *testing.T) {
|
|||||||
should.Equal(`{"1":"2"}`, str)
|
should.Equal(`{"1":"2"}`, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func Test_map_key_with_escaped_char(t *testing.T) {
|
func Test_map_key_with_escaped_char(t *testing.T) {
|
||||||
type Ttest struct {
|
type Ttest struct {
|
||||||
Map map[string]string
|
Map map[string]string
|
||||||
@ -119,11 +117,25 @@ func Test_map_key_with_escaped_char(t *testing.T) {
|
|||||||
{
|
{
|
||||||
var obj Ttest
|
var obj Ttest
|
||||||
should.Nil(json.Unmarshal(jsonBytes, &obj))
|
should.Nil(json.Unmarshal(jsonBytes, &obj))
|
||||||
should.Equal(map[string]string{"k\"ey":"val"}, obj.Map)
|
should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
var obj Ttest
|
var obj Ttest
|
||||||
should.Nil(Unmarshal(jsonBytes, &obj))
|
should.Nil(Unmarshal(jsonBytes, &obj))
|
||||||
should.Equal(map[string]string{"k\"ey":"val"}, obj.Map)
|
should.Equal(map[string]string{"k\"ey": "val"}, obj.Map)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_encode_map_with_sorted_keys(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
m := map[string]interface{}{
|
||||||
|
"3": 3,
|
||||||
|
"1": 1,
|
||||||
|
"2": 2,
|
||||||
|
}
|
||||||
|
bytes, err := json.Marshal(m)
|
||||||
|
should.Nil(err)
|
||||||
|
output, err := ConfigCompatibleWithStandardLibrary.MarshalToString(m)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(string(bytes), output)
|
||||||
|
}
|
||||||
|
@ -15,7 +15,7 @@ type Level2 struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_nested(t *testing.T) {
|
func Test_nested(t *testing.T) {
|
||||||
iter := ParseString(`{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
iter := ParseString(ConfigOfDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||||
l1 := Level1{}
|
l1 := Level1{}
|
||||||
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
||||||
switch l1Field {
|
switch l1Field {
|
||||||
@ -50,7 +50,7 @@ func Test_nested(t *testing.T) {
|
|||||||
|
|
||||||
func Benchmark_jsoniter_nested(b *testing.B) {
|
func Benchmark_jsoniter_nested(b *testing.B) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter := ParseString(`{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
iter := ParseString(ConfigOfDefault, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||||
l1 := Level1{}
|
l1 := Level1{}
|
||||||
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
||||||
switch l1Field {
|
switch l1Field {
|
||||||
|
@ -2,17 +2,18 @@ package jsoniter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_null(t *testing.T) {
|
func Test_read_null(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`null`)
|
iter := ParseString(ConfigOfDefault, `null`)
|
||||||
should.True(iter.ReadNil())
|
should.True(iter.ReadNil())
|
||||||
iter = ParseString(`null`)
|
iter = ParseString(ConfigOfDefault, `null`)
|
||||||
should.Nil(iter.Read())
|
should.Nil(iter.Read())
|
||||||
iter = ParseString(`null`)
|
iter = ParseString(ConfigOfDefault, `null`)
|
||||||
any, err := UnmarshalAnyFromString(`null`)
|
any, err := UnmarshalAnyFromString(`null`)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(0, any.ToInt())
|
should.Equal(0, any.ToInt())
|
||||||
@ -24,7 +25,7 @@ func Test_read_null(t *testing.T) {
|
|||||||
func Test_write_null(t *testing.T) {
|
func Test_write_null(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteNil()
|
stream.WriteNil()
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -40,7 +41,7 @@ func Test_encode_null(t *testing.T) {
|
|||||||
|
|
||||||
func Test_decode_null_object(t *testing.T) {
|
func Test_decode_null_object(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`[null,"a"]`)
|
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
if iter.ReadObject() != "" {
|
if iter.ReadObject() != "" {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -58,7 +59,7 @@ func Test_decode_null_object(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_decode_null_array(t *testing.T) {
|
func Test_decode_null_array(t *testing.T) {
|
||||||
iter := ParseString(`[null,"a"]`)
|
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
if iter.ReadArray() != false {
|
if iter.ReadArray() != false {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
@ -71,7 +72,7 @@ func Test_decode_null_array(t *testing.T) {
|
|||||||
|
|
||||||
func Test_decode_null_string(t *testing.T) {
|
func Test_decode_null_string(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`[null,"a"]`)
|
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
should.Equal("", iter.ReadString())
|
should.Equal("", iter.ReadString())
|
||||||
should.True(iter.ReadArray())
|
should.True(iter.ReadArray())
|
||||||
@ -79,7 +80,7 @@ func Test_decode_null_string(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_decode_null_skip(t *testing.T) {
|
func Test_decode_null_skip(t *testing.T) {
|
||||||
iter := ParseString(`[null,"a"]`)
|
iter := ParseString(ConfigOfDefault, `[null,"a"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -87,3 +88,39 @@ func Test_decode_null_skip(t *testing.T) {
|
|||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_encode_nil_map(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
type Ttest map[string]string
|
||||||
|
var obj1 Ttest
|
||||||
|
output, err := json.Marshal(obj1)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("null", string(output))
|
||||||
|
output, err = json.Marshal(&obj1)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("null", string(output))
|
||||||
|
output, err = Marshal(obj1)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("null", string(output))
|
||||||
|
output, err = Marshal(&obj1)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("null", string(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_encode_nil_array(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
type Ttest []string
|
||||||
|
var obj1 Ttest
|
||||||
|
output, err := json.Marshal(obj1)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("null", string(output))
|
||||||
|
output, err = json.Marshal(&obj1)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("null", string(output))
|
||||||
|
output, err = Marshal(obj1)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("null", string(output))
|
||||||
|
output, err = Marshal(&obj1)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal("null", string(output))
|
||||||
|
}
|
||||||
|
@ -9,10 +9,10 @@ import (
|
|||||||
|
|
||||||
func Test_empty_object(t *testing.T) {
|
func Test_empty_object(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{}`)
|
iter := ParseString(ConfigOfDefault, `{}`)
|
||||||
field := iter.ReadObject()
|
field := iter.ReadObject()
|
||||||
should.Equal("", field)
|
should.Equal("", field)
|
||||||
iter = ParseString(`{}`)
|
iter = ParseString(ConfigOfDefault, `{}`)
|
||||||
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||||
should.FailNow("should not call")
|
should.FailNow("should not call")
|
||||||
return true
|
return true
|
||||||
@ -21,14 +21,14 @@ func Test_empty_object(t *testing.T) {
|
|||||||
|
|
||||||
func Test_one_field(t *testing.T) {
|
func Test_one_field(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{"a": "b"}`)
|
iter := ParseString(ConfigOfDefault, `{"a": "b"}`)
|
||||||
field := iter.ReadObject()
|
field := iter.ReadObject()
|
||||||
should.Equal("a", field)
|
should.Equal("a", field)
|
||||||
value := iter.ReadString()
|
value := iter.ReadString()
|
||||||
should.Equal("b", value)
|
should.Equal("b", value)
|
||||||
field = iter.ReadObject()
|
field = iter.ReadObject()
|
||||||
should.Equal("", field)
|
should.Equal("", field)
|
||||||
iter = ParseString(`{"a": "b"}`)
|
iter = ParseString(ConfigOfDefault, `{"a": "b"}`)
|
||||||
should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||||
should.Equal("a", field)
|
should.Equal("a", field)
|
||||||
return true
|
return true
|
||||||
@ -37,7 +37,7 @@ func Test_one_field(t *testing.T) {
|
|||||||
|
|
||||||
func Test_two_field(t *testing.T) {
|
func Test_two_field(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`{ "a": "b" , "c": "d" }`)
|
iter := ParseString(ConfigOfDefault, `{ "a": "b" , "c": "d" }`)
|
||||||
field := iter.ReadObject()
|
field := iter.ReadObject()
|
||||||
should.Equal("a", field)
|
should.Equal("a", field)
|
||||||
value := iter.ReadString()
|
value := iter.ReadString()
|
||||||
@ -48,7 +48,7 @@ func Test_two_field(t *testing.T) {
|
|||||||
should.Equal("d", value)
|
should.Equal("d", value)
|
||||||
field = iter.ReadObject()
|
field = iter.ReadObject()
|
||||||
should.Equal("", field)
|
should.Equal("", field)
|
||||||
iter = ParseString(`{"field1": "1", "field2": 2}`)
|
iter = ParseString(ConfigOfDefault, `{"field1": "1", "field2": 2}`)
|
||||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||||
switch field {
|
switch field {
|
||||||
case "field1":
|
case "field1":
|
||||||
@ -210,8 +210,7 @@ func Test_object_wrapper_any_get_all(t *testing.T) {
|
|||||||
func Test_write_object(t *testing.T) {
|
func Test_write_object(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(Config{IndentionStep: 2}.Froze(), buf, 4096)
|
||||||
stream.IndentionStep = 2
|
|
||||||
stream.WriteObjectStart()
|
stream.WriteObjectStart()
|
||||||
stream.WriteObjectField("hello")
|
stream.WriteObjectField("hello")
|
||||||
stream.WriteInt(1)
|
stream.WriteInt(1)
|
||||||
@ -230,7 +229,7 @@ func Benchmark_jsoniter_object(b *testing.B) {
|
|||||||
Field2 uint64
|
Field2 uint64
|
||||||
}
|
}
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter := ParseString(`{"field1": "1", "field2": 2}`)
|
iter := ParseString(ConfigOfDefault, `{"field1": "1", "field2": 2}`)
|
||||||
obj := TestObj{}
|
obj := TestObj{}
|
||||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||||
switch field {
|
switch field {
|
||||||
|
@ -21,26 +21,26 @@ func Test_encode_optional_int_pointer(t *testing.T) {
|
|||||||
func Test_decode_struct_with_optional_field(t *testing.T) {
|
func Test_decode_struct_with_optional_field(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
field1 *string
|
Field1 *string
|
||||||
field2 *string
|
Field2 *string
|
||||||
}
|
}
|
||||||
obj := TestObject{}
|
obj := TestObject{}
|
||||||
UnmarshalFromString(`{"field1": null, "field2": "world"}`, &obj)
|
UnmarshalFromString(`{"field1": null, "field2": "world"}`, &obj)
|
||||||
should.Nil(obj.field1)
|
should.Nil(obj.Field1)
|
||||||
should.Equal("world", *obj.field2)
|
should.Equal("world", *obj.Field2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_encode_struct_with_optional_field(t *testing.T) {
|
func Test_encode_struct_with_optional_field(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
type TestObject struct {
|
type TestObject struct {
|
||||||
field1 *string
|
Field1 *string
|
||||||
field2 *string
|
Field2 *string
|
||||||
}
|
}
|
||||||
obj := TestObject{}
|
obj := TestObject{}
|
||||||
world := "world"
|
world := "world"
|
||||||
obj.field2 = &world
|
obj.Field2 = &world
|
||||||
str, err := MarshalToString(obj)
|
str, err := MarshalToString(obj)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Contains(str, `"field1":null`)
|
should.Contains(str, `"Field1":null`)
|
||||||
should.Contains(str, `"field2":"world"`)
|
should.Contains(str, `"Field2":"world"`)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_reflect_str(t *testing.T) {
|
func Test_reflect_str(t *testing.T) {
|
||||||
iter := ParseString(`"hello"`)
|
iter := ParseString(ConfigOfDefault, `"hello"`)
|
||||||
str := ""
|
str := ""
|
||||||
iter.ReadVal(&str)
|
iter.ReadVal(&str)
|
||||||
if str != "hello" {
|
if str != "hello" {
|
||||||
@ -16,7 +16,7 @@ func Test_reflect_str(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_ptr_str(t *testing.T) {
|
func Test_reflect_ptr_str(t *testing.T) {
|
||||||
iter := ParseString(`"hello"`)
|
iter := ParseString(ConfigOfDefault, `"hello"`)
|
||||||
var str *string
|
var str *string
|
||||||
iter.ReadVal(&str)
|
iter.ReadVal(&str)
|
||||||
if *str != "hello" {
|
if *str != "hello" {
|
||||||
@ -25,7 +25,7 @@ func Test_reflect_ptr_str(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int(t *testing.T) {
|
func Test_reflect_int(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := int(0)
|
val := int(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -34,7 +34,7 @@ func Test_reflect_int(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int8(t *testing.T) {
|
func Test_reflect_int8(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := int8(0)
|
val := int8(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -43,7 +43,7 @@ func Test_reflect_int8(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int16(t *testing.T) {
|
func Test_reflect_int16(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := int16(0)
|
val := int16(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -52,7 +52,7 @@ func Test_reflect_int16(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int32(t *testing.T) {
|
func Test_reflect_int32(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := int32(0)
|
val := int32(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -61,7 +61,7 @@ func Test_reflect_int32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_int64(t *testing.T) {
|
func Test_reflect_int64(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := int64(0)
|
val := int64(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -70,7 +70,7 @@ func Test_reflect_int64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint(t *testing.T) {
|
func Test_reflect_uint(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := uint(0)
|
val := uint(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -79,7 +79,7 @@ func Test_reflect_uint(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint8(t *testing.T) {
|
func Test_reflect_uint8(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := uint8(0)
|
val := uint8(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -88,7 +88,7 @@ func Test_reflect_uint8(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint16(t *testing.T) {
|
func Test_reflect_uint16(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := uint16(0)
|
val := uint16(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -97,7 +97,7 @@ func Test_reflect_uint16(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint32(t *testing.T) {
|
func Test_reflect_uint32(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := uint32(0)
|
val := uint32(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -106,7 +106,7 @@ func Test_reflect_uint32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_uint64(t *testing.T) {
|
func Test_reflect_uint64(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := uint64(0)
|
val := uint64(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -115,7 +115,7 @@ func Test_reflect_uint64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_byte(t *testing.T) {
|
func Test_reflect_byte(t *testing.T) {
|
||||||
iter := ParseString(`123`)
|
iter := ParseString(ConfigOfDefault, `123`)
|
||||||
val := byte(0)
|
val := byte(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 123 {
|
if val != 123 {
|
||||||
@ -124,7 +124,7 @@ func Test_reflect_byte(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_float32(t *testing.T) {
|
func Test_reflect_float32(t *testing.T) {
|
||||||
iter := ParseString(`1.23`)
|
iter := ParseString(ConfigOfDefault, `1.23`)
|
||||||
val := float32(0)
|
val := float32(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 1.23 {
|
if val != 1.23 {
|
||||||
@ -134,7 +134,7 @@ func Test_reflect_float32(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_float64(t *testing.T) {
|
func Test_reflect_float64(t *testing.T) {
|
||||||
iter := ParseString(`1.23`)
|
iter := ParseString(ConfigOfDefault, `1.23`)
|
||||||
val := float64(0)
|
val := float64(0)
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != 1.23 {
|
if val != 1.23 {
|
||||||
@ -144,7 +144,7 @@ func Test_reflect_float64(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_reflect_bool(t *testing.T) {
|
func Test_reflect_bool(t *testing.T) {
|
||||||
iter := ParseString(`true`)
|
iter := ParseString(ConfigOfDefault, `true`)
|
||||||
val := false
|
val := false
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if val != true {
|
if val != true {
|
||||||
|
@ -24,17 +24,17 @@ func Test_decode_large_slice(t *testing.T) {
|
|||||||
|
|
||||||
func Test_decode_nested(t *testing.T) {
|
func Test_decode_nested(t *testing.T) {
|
||||||
type StructOfString struct {
|
type StructOfString struct {
|
||||||
field1 string
|
Field1 string
|
||||||
field2 string
|
Field2 string
|
||||||
}
|
}
|
||||||
iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
|
iter := ParseString(ConfigOfDefault, `[{"field1": "hello"}, null, {"field2": "world"}]`)
|
||||||
slice := []*StructOfString{}
|
slice := []*StructOfString{}
|
||||||
iter.ReadVal(&slice)
|
iter.ReadVal(&slice)
|
||||||
if len(slice) != 3 {
|
if len(slice) != 3 {
|
||||||
fmt.Println(iter.Error)
|
fmt.Println(iter.Error)
|
||||||
t.Fatal(len(slice))
|
t.Fatal(len(slice))
|
||||||
}
|
}
|
||||||
if slice[0].field1 != "hello" {
|
if slice[0].Field1 != "hello" {
|
||||||
fmt.Println(iter.Error)
|
fmt.Println(iter.Error)
|
||||||
t.Fatal(slice[0])
|
t.Fatal(slice[0])
|
||||||
}
|
}
|
||||||
@ -42,19 +42,19 @@ func Test_decode_nested(t *testing.T) {
|
|||||||
fmt.Println(iter.Error)
|
fmt.Println(iter.Error)
|
||||||
t.Fatal(slice[1])
|
t.Fatal(slice[1])
|
||||||
}
|
}
|
||||||
if slice[2].field2 != "world" {
|
if slice[2].Field2 != "world" {
|
||||||
fmt.Println(iter.Error)
|
fmt.Println(iter.Error)
|
||||||
t.Fatal(slice[2])
|
t.Fatal(slice[2])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_decode_base64(t *testing.T) {
|
func Test_decode_base64(t *testing.T) {
|
||||||
iter := ParseString(`"YWJj"`)
|
iter := ParseString(ConfigOfDefault, `"YWJj"`)
|
||||||
val := []byte{}
|
val := []byte{}
|
||||||
RegisterTypeDecoder("[]uint8", func(ptr unsafe.Pointer, iter *Iterator) {
|
RegisterTypeDecoder("[]uint8", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||||
*((*[]byte)(ptr)) = iter.ReadBase64()
|
*((*[]byte)(ptr)) = iter.ReadBase64()
|
||||||
})
|
})
|
||||||
defer CleanDecoders()
|
defer ConfigOfDefault.CleanDecoders()
|
||||||
iter.ReadVal(&val)
|
iter.ReadVal(&val)
|
||||||
if "abc" != string(val) {
|
if "abc" != string(val) {
|
||||||
t.Fatal(string(val))
|
t.Fatal(string(val))
|
||||||
@ -70,7 +70,7 @@ type StructOfTagOne struct {
|
|||||||
|
|
||||||
func Benchmark_jsoniter_reflect(b *testing.B) {
|
func Benchmark_jsoniter_reflect(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
iter := NewIterator()
|
iter := NewIterator(ConfigOfDefault)
|
||||||
Struct := &StructOfTagOne{}
|
Struct := &StructOfTagOne{}
|
||||||
//var Struct *StructOfTagOne
|
//var Struct *StructOfTagOne
|
||||||
input := []byte(`{"field3": "100", "field4": "100"}`)
|
input := []byte(`{"field3": "100", "field4": "100"}`)
|
||||||
@ -96,7 +96,7 @@ func Benchmark_jsoniter_direct(b *testing.B) {
|
|||||||
// iter.Skip()
|
// iter.Skip()
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
iter := ParseString(`["hello", "world"]`)
|
iter := ParseString(ConfigOfDefault, `["hello", "world"]`)
|
||||||
array := make([]string, 0, 2)
|
array := make([]string, 0, 2)
|
||||||
for iter.ReadArray() {
|
for iter.ReadArray() {
|
||||||
array = append(array, iter.ReadString())
|
array = append(array, iter.ReadString())
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_skip_number(t *testing.T) {
|
func Test_skip_number(t *testing.T) {
|
||||||
iter := ParseString(`[-0.12, "b"]`)
|
iter := ParseString(ConfigOfDefault, `[-0.12, "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -16,7 +16,7 @@ func Test_skip_number(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_null(t *testing.T) {
|
func Test_skip_null(t *testing.T) {
|
||||||
iter := ParseString(`[null , "b"]`)
|
iter := ParseString(ConfigOfDefault, `[null , "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -26,7 +26,7 @@ func Test_skip_null(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_true(t *testing.T) {
|
func Test_skip_true(t *testing.T) {
|
||||||
iter := ParseString(`[true , "b"]`)
|
iter := ParseString(ConfigOfDefault, `[true , "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -36,7 +36,7 @@ func Test_skip_true(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_false(t *testing.T) {
|
func Test_skip_false(t *testing.T) {
|
||||||
iter := ParseString(`[false , "b"]`)
|
iter := ParseString(ConfigOfDefault, `[false , "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -46,7 +46,7 @@ func Test_skip_false(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_array(t *testing.T) {
|
func Test_skip_array(t *testing.T) {
|
||||||
iter := ParseString(`[[1, [2, [3], 4]], "b"]`)
|
iter := ParseString(ConfigOfDefault, `[[1, [2, [3], 4]], "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -56,7 +56,7 @@ func Test_skip_array(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_empty_array(t *testing.T) {
|
func Test_skip_empty_array(t *testing.T) {
|
||||||
iter := ParseString(`[ [ ], "b"]`)
|
iter := ParseString(ConfigOfDefault, `[ [ ], "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -66,7 +66,7 @@ func Test_skip_empty_array(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_skip_nested(t *testing.T) {
|
func Test_skip_nested(t *testing.T) {
|
||||||
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
iter := ParseString(ConfigOfDefault, `[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
iter.Skip()
|
iter.Skip()
|
||||||
iter.ReadArray()
|
iter.ReadArray()
|
||||||
@ -106,7 +106,7 @@ func Benchmark_jsoniter_skip(b *testing.B) {
|
|||||||
}`)
|
}`)
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
result := TestResp{}
|
result := TestResp{}
|
||||||
iter := ParseBytes(input)
|
iter := ParseBytes(ConfigOfDefault, input)
|
||||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||||
switch field {
|
switch field {
|
||||||
case "code":
|
case "code":
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
func Test_writeByte_should_grow_buffer(t *testing.T) {
|
func Test_writeByte_should_grow_buffer(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
stream := NewStream(nil, 1)
|
stream := NewStream(ConfigOfDefault, nil, 1)
|
||||||
stream.writeByte('1')
|
stream.writeByte('1')
|
||||||
should.Equal("1", string(stream.Buffer()))
|
should.Equal("1", string(stream.Buffer()))
|
||||||
should.Equal(1, len(stream.buf))
|
should.Equal(1, len(stream.buf))
|
||||||
@ -20,7 +20,7 @@ func Test_writeByte_should_grow_buffer(t *testing.T) {
|
|||||||
|
|
||||||
func Test_writeBytes_should_grow_buffer(t *testing.T) {
|
func Test_writeBytes_should_grow_buffer(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
stream := NewStream(nil, 1)
|
stream := NewStream(ConfigOfDefault, nil, 1)
|
||||||
stream.Write([]byte{'1', '2'})
|
stream.Write([]byte{'1', '2'})
|
||||||
should.Equal("12", string(stream.Buffer()))
|
should.Equal("12", string(stream.Buffer()))
|
||||||
should.Equal(3, len(stream.buf))
|
should.Equal(3, len(stream.buf))
|
||||||
@ -31,15 +31,14 @@ func Test_writeBytes_should_grow_buffer(t *testing.T) {
|
|||||||
|
|
||||||
func Test_writeIndention_should_grow_buffer(t *testing.T) {
|
func Test_writeIndention_should_grow_buffer(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
stream := NewStream(nil, 1)
|
stream := NewStream(Config{IndentionStep: 2}.Froze(), nil, 1)
|
||||||
stream.IndentionStep = 2
|
|
||||||
stream.WriteVal([]int{1, 2, 3})
|
stream.WriteVal([]int{1, 2, 3})
|
||||||
should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
|
should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_writeRaw_should_grow_buffer(t *testing.T) {
|
func Test_writeRaw_should_grow_buffer(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
stream := NewStream(nil, 1)
|
stream := NewStream(ConfigOfDefault, nil, 1)
|
||||||
stream.WriteRaw("123")
|
stream.WriteRaw("123")
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal("123", string(stream.Buffer()))
|
should.Equal("123", string(stream.Buffer()))
|
||||||
@ -47,7 +46,7 @@ func Test_writeRaw_should_grow_buffer(t *testing.T) {
|
|||||||
|
|
||||||
func Test_writeString_should_grow_buffer(t *testing.T) {
|
func Test_writeString_should_grow_buffer(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
stream := NewStream(nil, 0)
|
stream := NewStream(ConfigOfDefault, nil, 0)
|
||||||
stream.WriteString("123")
|
stream.WriteString("123")
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
should.Equal(`"123"`, string(stream.Buffer()))
|
should.Equal(`"123"`, string(stream.Buffer()))
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/json-iterator/go/require"
|
"github.com/json-iterator/go/require"
|
||||||
"testing"
|
"testing"
|
||||||
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_normal_string(t *testing.T) {
|
func Test_read_normal_string(t *testing.T) {
|
||||||
@ -17,22 +18,22 @@ func Test_read_normal_string(t *testing.T) {
|
|||||||
for input, output := range cases {
|
for input, output := range cases {
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigOfDefault, input)
|
||||||
should.Equal(output, iter.ReadString())
|
should.Equal(output, iter.ReadString())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
|
||||||
should.Equal(output, iter.ReadString())
|
should.Equal(output, iter.ReadString())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigOfDefault, input)
|
||||||
should.Equal(output, string(iter.ReadStringAsSlice()))
|
should.Equal(output, string(iter.ReadStringAsSlice()))
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
|
||||||
should.Equal(output, string(iter.ReadStringAsSlice()))
|
should.Equal(output, string(iter.ReadStringAsSlice()))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -48,12 +49,12 @@ func Test_read_exotic_string(t *testing.T) {
|
|||||||
for input, output := range cases {
|
for input, output := range cases {
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(input)
|
iter := ParseString(ConfigOfDefault, input)
|
||||||
should.Equal(output, iter.ReadString())
|
should.Equal(output, iter.ReadString())
|
||||||
})
|
})
|
||||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := Parse(bytes.NewBufferString(input), 2)
|
iter := Parse(ConfigOfDefault, bytes.NewBufferString(input), 2)
|
||||||
should.Equal(output, iter.ReadString())
|
should.Equal(output, iter.ReadString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -61,7 +62,7 @@ func Test_read_exotic_string(t *testing.T) {
|
|||||||
|
|
||||||
func Test_read_string_as_interface(t *testing.T) {
|
func Test_read_string_as_interface(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
iter := ParseString(`"hello"`)
|
iter := ParseString(ConfigOfDefault, `"hello"`)
|
||||||
should.Equal("hello", iter.Read())
|
should.Equal("hello", iter.Read())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,7 +99,7 @@ func Test_write_string(t *testing.T) {
|
|||||||
func Test_write_val_string(t *testing.T) {
|
func Test_write_val_string(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 4096)
|
stream := NewStream(ConfigOfDefault, buf, 4096)
|
||||||
stream.WriteVal("hello")
|
stream.WriteVal("hello")
|
||||||
stream.Flush()
|
stream.Flush()
|
||||||
should.Nil(stream.Error)
|
should.Nil(stream.Error)
|
||||||
@ -112,15 +113,57 @@ func Test_decode_slash(t *testing.T) {
|
|||||||
should.NotNil(UnmarshalFromString("\\", &obj))
|
should.NotNil(UnmarshalFromString("\\", &obj))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_html_escape(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
output, err := json.Marshal(`>`)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"\u003e"`, string(output))
|
||||||
|
output, err = ConfigCompatibleWithStandardLibrary.Marshal(`>`)
|
||||||
|
should.Nil(err)
|
||||||
|
should.Equal(`"\u003e"`, string(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_string_encode_with_std(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
for i := 0; i < utf8.RuneSelf; i++ {
|
||||||
|
input := string([]byte{byte(i)})
|
||||||
|
stdOutputBytes, err := json.Marshal(input)
|
||||||
|
should.Nil(err)
|
||||||
|
stdOutput := string(stdOutputBytes)
|
||||||
|
jsoniterOutputBytes, err := ConfigCompatibleWithStandardLibrary.Marshal(input)
|
||||||
|
should.Nil(err)
|
||||||
|
jsoniterOutput := string(jsoniterOutputBytes)
|
||||||
|
should.Equal(stdOutput, jsoniterOutput)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_string_encode_with_std_without_html_escape(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
for i := 0; i < utf8.RuneSelf; i++ {
|
||||||
|
input := string([]byte{byte(i)})
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
encoder := json.NewEncoder(buf)
|
||||||
|
encoder.SetEscapeHTML(false)
|
||||||
|
err := encoder.Encode(input)
|
||||||
|
should.Nil(err)
|
||||||
|
stdOutput := buf.String()
|
||||||
|
stdOutput = stdOutput[:len(stdOutput) - 1]
|
||||||
|
jsoniterOutputBytes, err := Marshal(input)
|
||||||
|
should.Nil(err)
|
||||||
|
jsoniterOutput := string(jsoniterOutputBytes)
|
||||||
|
should.Equal(stdOutput, jsoniterOutput)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_unicode(b *testing.B) {
|
func Benchmark_jsoniter_unicode(b *testing.B) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter := ParseString(`"\ud83d\udc4a"`)
|
iter := ParseString(ConfigOfDefault, `"\ud83d\udc4a"`)
|
||||||
iter.ReadString()
|
iter.ReadString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_ascii(b *testing.B) {
|
func Benchmark_jsoniter_ascii(b *testing.B) {
|
||||||
iter := NewIterator()
|
iter := NewIterator(ConfigOfDefault)
|
||||||
input := []byte(`"hello, world! hello, world!"`)
|
input := []byte(`"hello, world! hello, world!"`)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
@ -130,7 +173,7 @@ func Benchmark_jsoniter_ascii(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
|
func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
|
||||||
iter := ParseString(`"hello, world!"`)
|
iter := ParseString(ConfigOfDefault, `"hello, world!"`)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
iter.ResetBytes(iter.buf)
|
iter.ResetBytes(iter.buf)
|
||||||
|
144
output_tests/builtins/bool/json_test.go
Normal file
144
output_tests/builtins/bool/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/bool/types.go
Normal file
3
output_tests/builtins/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T bool
|
144
output_tests/builtins/bool_alias/json_test.go
Normal file
144
output_tests/builtins/bool_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/bool_alias/types.go
Normal file
3
output_tests/builtins/bool_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T bool
|
144
output_tests/builtins/byte/json_test.go
Normal file
144
output_tests/builtins/byte/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/byte/types.go
Normal file
3
output_tests/builtins/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T byte
|
144
output_tests/builtins/byte_alias/json_test.go
Normal file
144
output_tests/builtins/byte_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/byte_alias/types.go
Normal file
3
output_tests/builtins/byte_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T byte
|
144
output_tests/builtins/float32/json_test.go
Normal file
144
output_tests/builtins/float32/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/float32/types.go
Normal file
3
output_tests/builtins/float32/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T float32
|
144
output_tests/builtins/float32_alias/json_test.go
Normal file
144
output_tests/builtins/float32_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/float32_alias/types.go
Normal file
3
output_tests/builtins/float32_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T float32
|
144
output_tests/builtins/float64/json_test.go
Normal file
144
output_tests/builtins/float64/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/float64/types.go
Normal file
3
output_tests/builtins/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T float64
|
144
output_tests/builtins/float64_alias/json_test.go
Normal file
144
output_tests/builtins/float64_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/float64_alias/types.go
Normal file
3
output_tests/builtins/float64_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T float64
|
144
output_tests/builtins/int16/json_test.go
Normal file
144
output_tests/builtins/int16/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/int16/types.go
Normal file
3
output_tests/builtins/int16/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T int16
|
144
output_tests/builtins/int16_alias/json_test.go
Normal file
144
output_tests/builtins/int16_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/int16_alias/types.go
Normal file
3
output_tests/builtins/int16_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T int16
|
144
output_tests/builtins/int32/json_test.go
Normal file
144
output_tests/builtins/int32/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/int32/types.go
Normal file
3
output_tests/builtins/int32/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T int32
|
144
output_tests/builtins/int32_alias/json_test.go
Normal file
144
output_tests/builtins/int32_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/int32_alias/types.go
Normal file
3
output_tests/builtins/int32_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T int32
|
144
output_tests/builtins/int8/json_test.go
Normal file
144
output_tests/builtins/int8/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/int8/types.go
Normal file
3
output_tests/builtins/int8/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T int8
|
144
output_tests/builtins/int8_alias/json_test.go
Normal file
144
output_tests/builtins/int8_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/int8_alias/types.go
Normal file
3
output_tests/builtins/int8_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T int8
|
144
output_tests/builtins/string/json_test.go
Normal file
144
output_tests/builtins/string/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/string/types.go
Normal file
3
output_tests/builtins/string/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T string
|
144
output_tests/builtins/string_alias/json_test.go
Normal file
144
output_tests/builtins/string_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/string_alias/types.go
Normal file
3
output_tests/builtins/string_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T string
|
144
output_tests/builtins/uint16/json_test.go
Normal file
144
output_tests/builtins/uint16/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/uint16/types.go
Normal file
3
output_tests/builtins/uint16/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T uint16
|
144
output_tests/builtins/uint16_alias/json_test.go
Normal file
144
output_tests/builtins/uint16_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/uint16_alias/types.go
Normal file
3
output_tests/builtins/uint16_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T uint16
|
144
output_tests/builtins/uint32/json_test.go
Normal file
144
output_tests/builtins/uint32/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/uint32/types.go
Normal file
3
output_tests/builtins/uint32/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T uint32
|
144
output_tests/builtins/uint32_alias/json_test.go
Normal file
144
output_tests/builtins/uint32_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/uint32_alias/types.go
Normal file
3
output_tests/builtins/uint32_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T uint32
|
144
output_tests/builtins/uint8/json_test.go
Normal file
144
output_tests/builtins/uint8/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/uint8/types.go
Normal file
3
output_tests/builtins/uint8/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T uint8
|
144
output_tests/builtins/uint8_alias/json_test.go
Normal file
144
output_tests/builtins/uint8_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/builtins/uint8_alias/types.go
Normal file
3
output_tests/builtins/uint8_alias/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T uint8
|
144
output_tests/maps/builtins/int32/bool/json_test.go
Normal file
144
output_tests/maps/builtins/int32/bool/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/maps/builtins/int32/bool/types.go
Normal file
3
output_tests/maps/builtins/int32/bool/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T map[int32]bool
|
144
output_tests/maps/builtins/int32/byte/json_test.go
Normal file
144
output_tests/maps/builtins/int32/byte/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/maps/builtins/int32/byte/types.go
Normal file
3
output_tests/maps/builtins/int32/byte/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T map[int32]byte
|
144
output_tests/maps/builtins/int32/float32/json_test.go
Normal file
144
output_tests/maps/builtins/int32/float32/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/maps/builtins/int32/float32/types.go
Normal file
3
output_tests/maps/builtins/int32/float32/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T map[int32]float32
|
144
output_tests/maps/builtins/int32/float64/json_test.go
Normal file
144
output_tests/maps/builtins/int32/float64/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/maps/builtins/int32/float64/types.go
Normal file
3
output_tests/maps/builtins/int32/float64/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T map[int32]float64
|
144
output_tests/maps/builtins/int32/int32/json_test.go
Normal file
144
output_tests/maps/builtins/int32/int32/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/maps/builtins/int32/int32/types.go
Normal file
3
output_tests/maps/builtins/int32/int32/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T map[int32]int32
|
144
output_tests/maps/builtins/int32/int8/json_test.go
Normal file
144
output_tests/maps/builtins/int32/int8/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/maps/builtins/int32/int8/types.go
Normal file
3
output_tests/maps/builtins/int32/int8/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T map[int32]int8
|
144
output_tests/maps/builtins/int32/string/json_test.go
Normal file
144
output_tests/maps/builtins/int32/string/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
output_tests/maps/builtins/int32/string/types.go
Normal file
3
output_tests/maps/builtins/int32/string/types.go
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type T map[int32]string
|
144
output_tests/maps/builtins/int32/string_alias/json_test.go
Normal file
144
output_tests/maps/builtins/int32/string_alias/json_test.go
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
fuzz "github.com/google/gofuzz"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Roundtrip(t *testing.T) {
|
||||||
|
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
var before T
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
|
||||||
|
jbStd, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
jbIter, err := jsoniter.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if string(jbStd) != string(jbIter) {
|
||||||
|
t.Errorf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||||
|
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||||
|
}
|
||||||
|
|
||||||
|
var afterStd T
|
||||||
|
err = json.Unmarshal(jbIter, &afterStd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with stdlib: %v", err)
|
||||||
|
}
|
||||||
|
var afterIter T
|
||||||
|
err = jsoniter.Unmarshal(jbIter, &afterIter)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal with jsoniter: %v", err)
|
||||||
|
}
|
||||||
|
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||||
|
t.Errorf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||||
|
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const indentStr = "> "
|
||||||
|
|
||||||
|
func fingerprint(obj interface{}) string {
|
||||||
|
c := spew.ConfigState{
|
||||||
|
SortKeys: true,
|
||||||
|
SpewKeys: true,
|
||||||
|
}
|
||||||
|
return c.Sprintf("%v", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dump(obj interface{}) string {
|
||||||
|
cfg := spew.ConfigState{
|
||||||
|
Indent: indentStr,
|
||||||
|
}
|
||||||
|
return cfg.Sdump(obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func indent(src []byte, prefix string) string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
json.Indent(&buf, src, prefix, indentStr)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := json.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = json.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterMarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var obj T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&obj)
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
jb, err := jsoniter.Marshal(obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal:\n input: %s\n error: %v", dump(obj), err)
|
||||||
|
}
|
||||||
|
_ = jb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkJSONIterUnmarshal(t *testing.B) {
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.ResetTimer()
|
||||||
|
|
||||||
|
var before T
|
||||||
|
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||||
|
fz.Fuzz(&before)
|
||||||
|
jb, err := json.Marshal(before)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < t.N; i++ {
|
||||||
|
var after T
|
||||||
|
err = jsoniter.Unmarshal(jb, &after)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to unmarshal:\n input: %q\n error: %v", string(jb), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
5
output_tests/maps/builtins/int32/string_alias/types.go
Normal file
5
output_tests/maps/builtins/int32/string_alias/types.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
type StringAlias string
|
||||||
|
|
||||||
|
type T map[int32]StringAlias
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user