mirror of
https://github.com/json-iterator/go.git
synced 2025-02-19 19:59:49 +02:00
#53 extract out config
This commit is contained in:
parent
788918b85d
commit
acddcf5bbf
@ -26,7 +26,7 @@ import (
|
||||
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
|
||||
func Unmarshal(data []byte, v interface{}) error {
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
iter := ParseBytes(DEFAULT_CONFIG, data)
|
||||
typ := reflect.TypeOf(v)
|
||||
if typ.Kind() != reflect.Ptr {
|
||||
// return non-pointer error
|
||||
@ -48,7 +48,7 @@ func Unmarshal(data []byte, v interface{}) error {
|
||||
// UnmarshalAny adapts to
|
||||
func UnmarshalAny(data []byte) (Any, error) {
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
iter := ParseBytes(DEFAULT_CONFIG, data)
|
||||
any := iter.ReadAny()
|
||||
if iter.head == iter.tail {
|
||||
iter.loadMore()
|
||||
@ -74,7 +74,7 @@ func lastNotSpacePos(data []byte) int {
|
||||
func UnmarshalFromString(str string, v interface{}) error {
|
||||
data := []byte(str)
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
iter := ParseBytes(DEFAULT_CONFIG, data)
|
||||
iter.ReadVal(v)
|
||||
if iter.head == iter.tail {
|
||||
iter.loadMore()
|
||||
@ -91,7 +91,7 @@ func UnmarshalFromString(str string, v interface{}) error {
|
||||
func UnmarshalAnyFromString(str string) (Any, error) {
|
||||
data := []byte(str)
|
||||
data = data[:lastNotSpacePos(data)]
|
||||
iter := ParseBytes(data)
|
||||
iter := ParseBytes(DEFAULT_CONFIG, data)
|
||||
any := iter.ReadAny()
|
||||
if iter.head == iter.tail {
|
||||
iter.loadMore()
|
||||
@ -110,7 +110,7 @@ func UnmarshalAnyFromString(str string) (Any, error) {
|
||||
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
|
||||
// Refer to https://godoc.org/encoding/json#Marshal for more information
|
||||
func Marshal(v interface{}) ([]byte, error) {
|
||||
stream := NewStream(nil, 256)
|
||||
stream := NewStream(DEFAULT_CONFIG, nil, 256)
|
||||
stream.WriteVal(v)
|
||||
if stream.Error != nil {
|
||||
return nil, stream.Error
|
||||
@ -133,7 +133,7 @@ func MarshalToString(v interface{}) (string, error) {
|
||||
// Instead of a json/encoding Decoder, an AdaptedDecoder is returned
|
||||
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
|
||||
func NewDecoder(reader io.Reader) *AdaptedDecoder {
|
||||
iter := Parse(reader, 512)
|
||||
iter := Parse(DEFAULT_CONFIG, reader, 512)
|
||||
return &AdaptedDecoder{iter}
|
||||
}
|
||||
|
||||
@ -172,7 +172,7 @@ func (decoder *AdaptedDecoder) UseNumber() {
|
||||
}
|
||||
|
||||
func NewEncoder(writer io.Writer) *AdaptedEncoder {
|
||||
stream := NewStream(writer, 512)
|
||||
stream := NewStream(DEFAULT_CONFIG, writer, 512)
|
||||
return &AdaptedEncoder{stream}
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ func (any *arrayLazyAny) ValueType() ValueType {
|
||||
func (any *arrayLazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
iter = NewIterator(DEFAULT_CONFIG)
|
||||
any.iter = iter
|
||||
}
|
||||
iter.ResetBytes(any.remaining)
|
||||
@ -287,7 +287,7 @@ func (any *arrayLazyAny) IterateArray() (func() (Any, bool), bool) {
|
||||
// read from buffer
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
iter = NewIterator(DEFAULT_CONFIG)
|
||||
any.iter = iter
|
||||
}
|
||||
iter.ResetBytes(remaining)
|
||||
|
@ -17,7 +17,7 @@ type float64LazyAny struct {
|
||||
func (any *float64LazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
iter = NewIterator(DEFAULT_CONFIG)
|
||||
}
|
||||
iter.ResetBytes(any.buf)
|
||||
return iter
|
||||
|
@ -21,7 +21,7 @@ func (any *int64LazyAny) ValueType() ValueType {
|
||||
func (any *int64LazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
iter = NewIterator(DEFAULT_CONFIG)
|
||||
}
|
||||
iter.ResetBytes(any.buf)
|
||||
return iter
|
||||
|
@ -22,7 +22,7 @@ func (any *objectLazyAny) ValueType() ValueType {
|
||||
func (any *objectLazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
iter = NewIterator(DEFAULT_CONFIG)
|
||||
any.iter = iter
|
||||
}
|
||||
iter.ResetBytes(any.remaining)
|
||||
@ -308,7 +308,7 @@ func (any *objectLazyAny) IterateObject() (func() (string, Any, bool), bool) {
|
||||
// read from buffer
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
iter = NewIterator(DEFAULT_CONFIG)
|
||||
any.iter = iter
|
||||
}
|
||||
iter.ResetBytes(remaining)
|
||||
|
@ -20,7 +20,7 @@ func (any *stringLazyAny) ValueType() ValueType {
|
||||
func (any *stringLazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
iter = NewIterator(DEFAULT_CONFIG)
|
||||
any.iter = iter
|
||||
}
|
||||
iter.ResetBytes(any.buf)
|
||||
|
@ -21,7 +21,7 @@ func (any *uint64LazyAny) ValueType() ValueType {
|
||||
func (any *uint64LazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
iter = NewIterator(DEFAULT_CONFIG)
|
||||
}
|
||||
iter.ResetBytes(any.buf)
|
||||
return iter
|
||||
|
76
feature_config.go
Normal file
76
feature_config.go
Normal file
@ -0,0 +1,76 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
decoderCache unsafe.Pointer
|
||||
encoderCache unsafe.Pointer
|
||||
}
|
||||
|
||||
var DEFAULT_CONFIG = &Config{}
|
||||
|
||||
func init() {
|
||||
initConfig(DEFAULT_CONFIG)
|
||||
}
|
||||
|
||||
func initConfig(cfg *Config) {
|
||||
atomic.StorePointer(&cfg.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
|
||||
atomic.StorePointer(&cfg.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
|
||||
}
|
||||
func (cfg *Config) 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 *Config) 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 *Config) getDecoderFromCache(cacheKey reflect.Type) Decoder {
|
||||
ptr := atomic.LoadPointer(&cfg.decoderCache)
|
||||
cache := *(*map[reflect.Type]Decoder)(ptr)
|
||||
return cache[cacheKey]
|
||||
}
|
||||
|
||||
func (cfg *Config) 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 *Config) 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 *Config) CleanEncoders() {
|
||||
typeEncoders = map[string]Encoder{}
|
||||
fieldEncoders = map[string]Encoder{}
|
||||
atomic.StorePointer(&cfg.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
|
||||
}
|
@ -66,6 +66,7 @@ func init() {
|
||||
|
||||
// Iterator is a fast and flexible JSON parser
|
||||
type Iterator struct {
|
||||
cfg *Config
|
||||
reader io.Reader
|
||||
buf []byte
|
||||
head int
|
||||
@ -74,8 +75,9 @@ type Iterator struct {
|
||||
}
|
||||
|
||||
// Create creates an empty Iterator instance
|
||||
func NewIterator() *Iterator {
|
||||
func NewIterator(cfg *Config) *Iterator {
|
||||
return &Iterator{
|
||||
cfg: cfg,
|
||||
reader: nil,
|
||||
buf: nil,
|
||||
head: 0,
|
||||
@ -84,8 +86,9 @@ func NewIterator() *Iterator {
|
||||
}
|
||||
|
||||
// Parse parses a json buffer in io.Reader into an Iterator instance
|
||||
func Parse(reader io.Reader, bufSize int) *Iterator {
|
||||
func Parse(cfg *Config, reader io.Reader, bufSize int) *Iterator {
|
||||
return &Iterator{
|
||||
cfg: cfg,
|
||||
reader: reader,
|
||||
buf: make([]byte, bufSize),
|
||||
head: 0,
|
||||
@ -94,8 +97,9 @@ func Parse(reader io.Reader, bufSize int) *Iterator {
|
||||
}
|
||||
|
||||
// ParseBytes parses a json byte slice into an Iterator instance
|
||||
func ParseBytes(input []byte) *Iterator {
|
||||
func ParseBytes(cfg *Config, input []byte) *Iterator {
|
||||
return &Iterator{
|
||||
cfg: cfg,
|
||||
reader: nil,
|
||||
buf: input,
|
||||
head: 0,
|
||||
@ -104,8 +108,8 @@ func ParseBytes(input []byte) *Iterator {
|
||||
}
|
||||
|
||||
// ParseString parses a json string into an Iterator instance
|
||||
func ParseString(input string) *Iterator {
|
||||
return ParseBytes([]byte(input))
|
||||
func ParseString(cfg *Config, input string) *Iterator {
|
||||
return ParseBytes(cfg, []byte(input))
|
||||
}
|
||||
|
||||
// Reset can reset an Iterator instance for another json buffer in io.Reader
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
@ -73,9 +72,6 @@ func (encoder *funcEncoder) isEmpty(ptr unsafe.Pointer) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
var DECODERS unsafe.Pointer
|
||||
var ENCODERS unsafe.Pointer
|
||||
|
||||
var typeDecoders map[string]Decoder
|
||||
var fieldDecoders map[string]Decoder
|
||||
var typeEncoders map[string]Encoder
|
||||
@ -94,8 +90,6 @@ func init() {
|
||||
typeEncoders = map[string]Encoder{}
|
||||
fieldEncoders = map[string]Encoder{}
|
||||
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()
|
||||
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
|
||||
anyType = reflect.TypeOf((*Any)(nil)).Elem()
|
||||
@ -104,46 +98,6 @@ func init() {
|
||||
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
|
||||
func RegisterTypeDecoder(typ string, fun DecoderFunc) {
|
||||
typeDecoders[typ] = &funcDecoder{fun}
|
||||
@ -167,20 +121,6 @@ func RegisterExtension(extension ExtensionFunc) {
|
||||
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 {
|
||||
valueType reflect.Type
|
||||
valueDecoder Decoder
|
||||
@ -274,15 +214,15 @@ type nonEmptyInterface struct {
|
||||
func (iter *Iterator) ReadVal(obj interface{}) {
|
||||
typ := reflect.TypeOf(obj)
|
||||
cacheKey := typ.Elem()
|
||||
cachedDecoder := getDecoderFromCache(cacheKey)
|
||||
cachedDecoder := iter.cfg.getDecoderFromCache(cacheKey)
|
||||
if cachedDecoder == nil {
|
||||
decoder, err := decoderOfType(cacheKey)
|
||||
decoder, err := decoderOfType(iter.cfg, cacheKey)
|
||||
if err != nil {
|
||||
iter.Error = err
|
||||
return
|
||||
}
|
||||
cachedDecoder = decoder
|
||||
addDecoderToCache(cacheKey, decoder)
|
||||
iter.cfg.addDecoderToCache(cacheKey, decoder)
|
||||
}
|
||||
e := (*emptyInterface)(unsafe.Pointer(&obj))
|
||||
cachedDecoder.decode(e.word, iter)
|
||||
@ -295,15 +235,15 @@ func (stream *Stream) WriteVal(val interface{}) {
|
||||
}
|
||||
typ := reflect.TypeOf(val)
|
||||
cacheKey := typ
|
||||
cachedEncoder := getEncoderFromCache(cacheKey)
|
||||
cachedEncoder := stream.cfg.getEncoderFromCache(cacheKey)
|
||||
if cachedEncoder == nil {
|
||||
encoder, err := encoderOfType(cacheKey)
|
||||
encoder, err := encoderOfType(stream.cfg, cacheKey)
|
||||
if err != nil {
|
||||
stream.Error = err
|
||||
return
|
||||
}
|
||||
cachedEncoder = encoder
|
||||
addEncoderToCache(cacheKey, encoder)
|
||||
stream.cfg.addEncoderToCache(cacheKey, encoder)
|
||||
}
|
||||
cachedEncoder.encodeInterface(val, stream)
|
||||
}
|
||||
@ -324,7 +264,7 @@ func (p prefix) addToEncoder(encoder Encoder, err error) (Encoder, error) {
|
||||
return encoder, err
|
||||
}
|
||||
|
||||
func decoderOfType(typ reflect.Type) (Decoder, error) {
|
||||
func decoderOfType(cfg *Config, typ reflect.Type) (Decoder, error) {
|
||||
typeName := typ.String()
|
||||
typeDecoder := typeDecoders[typeName]
|
||||
if typeDecoder != nil {
|
||||
@ -337,19 +277,19 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
|
||||
}
|
||||
}
|
||||
cacheKey := typ
|
||||
cachedDecoder := getDecoderFromCache(cacheKey)
|
||||
cachedDecoder := cfg.getDecoderFromCache(cacheKey)
|
||||
if cachedDecoder != nil {
|
||||
return cachedDecoder, nil
|
||||
}
|
||||
placeholder := &placeholderDecoder{}
|
||||
addDecoderToCache(cacheKey, placeholder)
|
||||
newDecoder, err := createDecoderOfType(typ)
|
||||
cfg.addDecoderToCache(cacheKey, placeholder)
|
||||
newDecoder, err := createDecoderOfType(cfg, typ)
|
||||
placeholder.valueDecoder = newDecoder
|
||||
addDecoderToCache(cacheKey, newDecoder)
|
||||
cfg.addDecoderToCache(cacheKey, newDecoder)
|
||||
return newDecoder, err
|
||||
}
|
||||
|
||||
func createDecoderOfType(typ reflect.Type) (Decoder, error) {
|
||||
func createDecoderOfType(cfg *Config, typ reflect.Type) (Decoder, error) {
|
||||
if typ.String() == "[]uint8" {
|
||||
return &base64Codec{}, nil
|
||||
}
|
||||
@ -402,19 +342,19 @@ func createDecoderOfType(typ reflect.Type) (Decoder, error) {
|
||||
return &nonEmptyInterfaceCodec{}, nil
|
||||
}
|
||||
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:
|
||||
return prefix("[slice]").addToDecoder(decoderOfSlice(typ))
|
||||
return prefix("[slice]").addToDecoder(decoderOfSlice(cfg, typ))
|
||||
case reflect.Map:
|
||||
return prefix("[map]").addToDecoder(decoderOfMap(typ))
|
||||
return prefix("[map]").addToDecoder(decoderOfMap(cfg, typ))
|
||||
case reflect.Ptr:
|
||||
return prefix("[optional]").addToDecoder(decoderOfOptional(typ))
|
||||
return prefix("[optional]").addToDecoder(decoderOfOptional(cfg, typ))
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported type: %v", typ)
|
||||
}
|
||||
}
|
||||
|
||||
func encoderOfType(typ reflect.Type) (Encoder, error) {
|
||||
func encoderOfType(cfg *Config, typ reflect.Type) (Encoder, error) {
|
||||
typeName := typ.String()
|
||||
typeEncoder := typeEncoders[typeName]
|
||||
if typeEncoder != nil {
|
||||
@ -427,19 +367,19 @@ func encoderOfType(typ reflect.Type) (Encoder, error) {
|
||||
}
|
||||
}
|
||||
cacheKey := typ
|
||||
cachedEncoder := getEncoderFromCache(cacheKey)
|
||||
cachedEncoder := cfg.getEncoderFromCache(cacheKey)
|
||||
if cachedEncoder != nil {
|
||||
return cachedEncoder, nil
|
||||
}
|
||||
placeholder := &placeholderEncoder{}
|
||||
addEncoderToCache(cacheKey, placeholder)
|
||||
newEncoder, err := createEncoderOfType(typ)
|
||||
cfg.addEncoderToCache(cacheKey, placeholder)
|
||||
newEncoder, err := createEncoderOfType(cfg, typ)
|
||||
placeholder.valueEncoder = newEncoder
|
||||
addEncoderToCache(cacheKey, newEncoder)
|
||||
cfg.addEncoderToCache(cacheKey, newEncoder)
|
||||
return newEncoder, err
|
||||
}
|
||||
|
||||
func createEncoderOfType(typ reflect.Type) (Encoder, error) {
|
||||
func createEncoderOfType(cfg *Config, typ reflect.Type) (Encoder, error) {
|
||||
if typ.String() == "[]uint8" {
|
||||
return &base64Codec{}, nil
|
||||
}
|
||||
@ -493,30 +433,30 @@ func createEncoderOfType(typ reflect.Type) (Encoder, error) {
|
||||
return &nonEmptyInterfaceCodec{}, nil
|
||||
}
|
||||
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:
|
||||
return prefix("[slice]").addToEncoder(encoderOfSlice(typ))
|
||||
return prefix("[slice]").addToEncoder(encoderOfSlice(cfg, typ))
|
||||
case reflect.Map:
|
||||
return prefix("[map]").addToEncoder(encoderOfMap(typ))
|
||||
return prefix("[map]").addToEncoder(encoderOfMap(cfg, typ))
|
||||
case reflect.Ptr:
|
||||
return prefix("[optional]").addToEncoder(encoderOfOptional(typ))
|
||||
return prefix("[optional]").addToEncoder(encoderOfOptional(cfg, typ))
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported type: %v", typ)
|
||||
}
|
||||
}
|
||||
|
||||
func decoderOfOptional(typ reflect.Type) (Decoder, error) {
|
||||
func decoderOfOptional(cfg *Config, typ reflect.Type) (Decoder, error) {
|
||||
elemType := typ.Elem()
|
||||
decoder, err := decoderOfType(elemType)
|
||||
decoder, err := decoderOfType(cfg, elemType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &optionalDecoder{elemType, decoder}, nil
|
||||
}
|
||||
|
||||
func encoderOfOptional(typ reflect.Type) (Encoder, error) {
|
||||
func encoderOfOptional(cfg *Config, typ reflect.Type) (Encoder, error) {
|
||||
elemType := typ.Elem()
|
||||
elemEncoder, err := encoderOfType(elemType)
|
||||
elemEncoder, err := encoderOfType(cfg, elemType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -527,8 +467,8 @@ func encoderOfOptional(typ reflect.Type) (Encoder, error) {
|
||||
return encoder, nil
|
||||
}
|
||||
|
||||
func decoderOfMap(typ reflect.Type) (Decoder, error) {
|
||||
decoder, err := decoderOfType(typ.Elem())
|
||||
func decoderOfMap(cfg *Config, typ reflect.Type) (Decoder, error) {
|
||||
decoder, err := decoderOfType(cfg, typ.Elem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -540,9 +480,9 @@ func extractInterface(val interface{}) emptyInterface {
|
||||
return *((*emptyInterface)(unsafe.Pointer(&val)))
|
||||
}
|
||||
|
||||
func encoderOfMap(typ reflect.Type) (Encoder, error) {
|
||||
func encoderOfMap(cfg *Config, typ reflect.Type) (Encoder, error) {
|
||||
elemType := typ.Elem()
|
||||
encoder, err := encoderOfType(elemType)
|
||||
encoder, err := encoderOfType(cfg, elemType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -7,16 +7,16 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func decoderOfSlice(typ reflect.Type) (Decoder, error) {
|
||||
decoder, err := decoderOfType(typ.Elem())
|
||||
func decoderOfSlice(cfg *Config, typ reflect.Type) (Decoder, error) {
|
||||
decoder, err := decoderOfType(cfg, typ.Elem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sliceDecoder{typ, typ.Elem(), decoder}, nil
|
||||
}
|
||||
|
||||
func encoderOfSlice(typ reflect.Type) (Encoder, error) {
|
||||
encoder, err := encoderOfType(typ.Elem())
|
||||
func encoderOfSlice(cfg *Config, typ reflect.Type) (Encoder, error) {
|
||||
encoder, err := encoderOfType(cfg, typ.Elem())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func encoderOfStruct(typ reflect.Type) (Encoder, error) {
|
||||
func encoderOfStruct(cfg *Config, typ reflect.Type) (Encoder, error) {
|
||||
structEncoder_ := &structEncoder{}
|
||||
fields := map[string]*structFieldEncoder{}
|
||||
for _, field := range listStructFields(typ) {
|
||||
@ -36,7 +36,7 @@ func encoderOfStruct(typ reflect.Type) (Encoder, error) {
|
||||
encoder := fieldEncoders[fieldEncoderKey]
|
||||
var err error
|
||||
if encoder == nil && len(fieldNames) > 0 {
|
||||
encoder, err = encoderOfType(field.Type)
|
||||
encoder, err = encoderOfType(cfg, field.Type)
|
||||
if err != nil {
|
||||
return prefix(fmt.Sprintf("{%s}", field.Name)).addToEncoder(encoder, err)
|
||||
}
|
||||
@ -71,7 +71,7 @@ func listStructFields(typ reflect.Type) []*reflect.StructField {
|
||||
return fields
|
||||
}
|
||||
|
||||
func decoderOfStruct(typ reflect.Type) (Decoder, error) {
|
||||
func decoderOfStruct(cfg *Config, typ reflect.Type) (Decoder, error) {
|
||||
fields := map[string]*structFieldDecoder{}
|
||||
for i := 0; i < typ.NumField(); i++ {
|
||||
field := typ.Field(i)
|
||||
@ -91,7 +91,7 @@ func decoderOfStruct(typ reflect.Type) (Decoder, error) {
|
||||
fieldNames := calcFieldNames(field.Name, tagParts[0], extensionProviedFieldNames)
|
||||
if decoder == nil && len(fieldNames) > 0 {
|
||||
var err error
|
||||
decoder, err = decoderOfType(field.Type)
|
||||
decoder, err = decoderOfType(cfg, field.Type)
|
||||
if err != nil {
|
||||
return prefix(fmt.Sprintf("{%s}", field.Name)).addToDecoder(decoder, err)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
)
|
||||
|
||||
type Stream struct {
|
||||
cfg *Config
|
||||
out io.Writer
|
||||
buf []byte
|
||||
n int
|
||||
@ -13,8 +14,16 @@ type Stream struct {
|
||||
IndentionStep int
|
||||
}
|
||||
|
||||
func NewStream(out io.Writer, bufSize int) *Stream {
|
||||
return &Stream{out, make([]byte, bufSize), 0, nil, 0, 0}
|
||||
func NewStream(cfg *Config, out io.Writer, bufSize int) *Stream {
|
||||
return &Stream{
|
||||
cfg: cfg,
|
||||
out: out,
|
||||
buf: make([]byte, bufSize),
|
||||
n: 0,
|
||||
Error: nil,
|
||||
indention: 0,
|
||||
IndentionStep: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Stream) Reset(out io.Writer) {
|
||||
|
@ -10,10 +10,10 @@ import (
|
||||
|
||||
func Test_empty_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`[]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[]`)
|
||||
cont := iter.ReadArray()
|
||||
should.False(cont)
|
||||
iter = ParseString(`[]`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `[]`)
|
||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||
should.FailNow("should not call")
|
||||
return true
|
||||
@ -22,11 +22,11 @@ func Test_empty_array(t *testing.T) {
|
||||
|
||||
func Test_one_element(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`[1]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[1]`)
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal(1, iter.ReadInt())
|
||||
should.False(iter.ReadArray())
|
||||
iter = ParseString(`[1]`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `[1]`)
|
||||
iter.ReadArrayCB(func(iter *Iterator) bool {
|
||||
should.Equal(1, iter.ReadInt())
|
||||
return true
|
||||
@ -35,13 +35,13 @@ func Test_one_element(t *testing.T) {
|
||||
|
||||
func Test_two_elements(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`[1,2]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[1,2]`)
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal(int64(1), iter.ReadInt64())
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal(int64(2), iter.ReadInt64())
|
||||
should.False(iter.ReadArray())
|
||||
iter = ParseString(`[1,2]`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `[1,2]`)
|
||||
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) {
|
||||
iter := ParseString(` [1]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, ` [1]`)
|
||||
cont := iter.ReadArray()
|
||||
if cont != true {
|
||||
t.FailNow()
|
||||
@ -163,7 +163,7 @@ func Test_whitespace_in_head(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_whitespace_after_array_start(t *testing.T) {
|
||||
iter := ParseString(`[ 1]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[ 1]`)
|
||||
cont := iter.ReadArray()
|
||||
if cont != true {
|
||||
t.FailNow()
|
||||
@ -174,7 +174,7 @@ func Test_whitespace_after_array_start(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_whitespace_before_array_end(t *testing.T) {
|
||||
iter := ParseString(`[1 ]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[1 ]`)
|
||||
cont := iter.ReadArray()
|
||||
if cont != true {
|
||||
t.FailNow()
|
||||
@ -189,7 +189,7 @@ func Test_whitespace_before_array_end(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_whitespace_before_comma(t *testing.T) {
|
||||
iter := ParseString(`[1 ,2]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[1 ,2]`)
|
||||
cont := iter.ReadArray()
|
||||
if cont != true {
|
||||
t.FailNow()
|
||||
@ -213,7 +213,7 @@ func Test_whitespace_before_comma(t *testing.T) {
|
||||
func Test_write_array(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.IndentionStep = 2
|
||||
stream.WriteArrayStart()
|
||||
stream.WriteInt(1)
|
||||
@ -288,7 +288,7 @@ func Test_decode_byte_array(t *testing.T) {
|
||||
func Benchmark_jsoniter_array(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
input := []byte(`[1,2,3,4,5,6,7,8,9]`)
|
||||
iter := ParseBytes(input)
|
||||
iter := ParseBytes(DEFAULT_CONFIG, input)
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
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) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`true`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `true`)
|
||||
should.True(iter.ReadBool())
|
||||
iter = ParseString(`true`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `true`)
|
||||
should.Equal(true, iter.Read())
|
||||
}
|
||||
|
||||
func Test_false(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`false`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `false`)
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteTrue()
|
||||
stream.WriteFalse()
|
||||
stream.Flush()
|
||||
@ -41,7 +41,7 @@ func Test_write_true_false(t *testing.T) {
|
||||
func Test_write_val_bool(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(true)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
|
@ -19,7 +19,7 @@ func Test_customize_type_decoder(t *testing.T) {
|
||||
}
|
||||
*((*time.Time)(ptr)) = t
|
||||
})
|
||||
defer CleanDecoders()
|
||||
defer DEFAULT_CONFIG.CleanDecoders()
|
||||
val := time.Time{}
|
||||
err := Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
|
||||
if err != nil {
|
||||
@ -37,7 +37,7 @@ func Test_customize_type_encoder(t *testing.T) {
|
||||
t := *((*time.Time)(ptr))
|
||||
stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
|
||||
})
|
||||
defer CleanEncoders()
|
||||
defer DEFAULT_CONFIG.CleanEncoders()
|
||||
val := time.Unix(0, 0)
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
@ -45,13 +45,13 @@ func Test_customize_type_encoder(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_customize_byte_array_encoder(t *testing.T) {
|
||||
CleanEncoders()
|
||||
DEFAULT_CONFIG.CleanEncoders()
|
||||
should := require.New(t)
|
||||
RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
|
||||
t := *((*[]byte)(ptr))
|
||||
stream.WriteString(string(t))
|
||||
})
|
||||
defer CleanEncoders()
|
||||
defer DEFAULT_CONFIG.CleanEncoders()
|
||||
val := []byte("abc")
|
||||
str, err := MarshalToString(val)
|
||||
should.Nil(err)
|
||||
@ -61,7 +61,7 @@ func Test_customize_byte_array_encoder(t *testing.T) {
|
||||
func Test_customize_float_marshal(t *testing.T) {
|
||||
should := require.New(t)
|
||||
EnableLossyFloatMarshalling()
|
||||
defer CleanEncoders()
|
||||
defer DEFAULT_CONFIG.CleanEncoders()
|
||||
str, err := MarshalToString(float32(1.23456789))
|
||||
should.Nil(err)
|
||||
should.Equal("1.234568", str)
|
||||
@ -75,7 +75,7 @@ func Test_customize_field_decoder(t *testing.T) {
|
||||
RegisterFieldDecoder("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
|
||||
})
|
||||
defer CleanDecoders()
|
||||
defer DEFAULT_CONFIG.CleanDecoders()
|
||||
tom := Tom{}
|
||||
err := Unmarshal([]byte(`{"field1": 100}`), &tom)
|
||||
if err != nil {
|
||||
|
@ -16,7 +16,7 @@ func Test_bind_api_demo(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_iterator_api_demo(t *testing.T) {
|
||||
iter := ParseString(`[0,1,2,3]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[0,1,2,3]`)
|
||||
total := 0
|
||||
for iter.ReadArray() {
|
||||
total += iter.ReadInt()
|
||||
|
@ -7,56 +7,56 @@ import (
|
||||
)
|
||||
|
||||
func Test_string_end(t *testing.T) {
|
||||
end, escaped := ParseString(`abc"`).findStringEnd()
|
||||
end, escaped := ParseString(DEFAULT_CONFIG, `abc"`).findStringEnd()
|
||||
if end != 4 {
|
||||
t.Fatal(end)
|
||||
}
|
||||
if escaped != false {
|
||||
t.Fatal(escaped)
|
||||
}
|
||||
end, escaped = ParseString(`abc\\"`).findStringEnd()
|
||||
end, escaped = ParseString(DEFAULT_CONFIG, `abc\\"`).findStringEnd()
|
||||
if end != 6 {
|
||||
t.Fatal(end)
|
||||
}
|
||||
if escaped != true {
|
||||
t.Fatal(escaped)
|
||||
}
|
||||
end, escaped = ParseString(`abc\\\\"`).findStringEnd()
|
||||
end, escaped = ParseString(DEFAULT_CONFIG, `abc\\\\"`).findStringEnd()
|
||||
if end != 8 {
|
||||
t.Fatal(end)
|
||||
}
|
||||
if escaped != true {
|
||||
t.Fatal(escaped)
|
||||
}
|
||||
end, escaped = ParseString(`abc\"`).findStringEnd()
|
||||
end, escaped = ParseString(DEFAULT_CONFIG, `abc\"`).findStringEnd()
|
||||
if end != -1 {
|
||||
t.Fatal(end)
|
||||
}
|
||||
if escaped != false {
|
||||
t.Fatal(escaped)
|
||||
}
|
||||
end, escaped = ParseString(`abc\`).findStringEnd()
|
||||
end, escaped = ParseString(DEFAULT_CONFIG, `abc\`).findStringEnd()
|
||||
if end != -1 {
|
||||
t.Fatal(end)
|
||||
}
|
||||
if escaped != true {
|
||||
t.Fatal(escaped)
|
||||
}
|
||||
end, escaped = ParseString(`abc\\`).findStringEnd()
|
||||
end, escaped = ParseString(DEFAULT_CONFIG, `abc\\`).findStringEnd()
|
||||
if end != -1 {
|
||||
t.Fatal(end)
|
||||
}
|
||||
if escaped != false {
|
||||
t.Fatal(escaped)
|
||||
}
|
||||
end, escaped = ParseString(`\\`).findStringEnd()
|
||||
end, escaped = ParseString(DEFAULT_CONFIG, `\\`).findStringEnd()
|
||||
if end != -1 {
|
||||
t.Fatal(end)
|
||||
}
|
||||
if escaped != false {
|
||||
t.Fatal(escaped)
|
||||
}
|
||||
end, escaped = ParseString(`\`).findStringEnd()
|
||||
end, escaped = ParseString(DEFAULT_CONFIG, `\`).findStringEnd()
|
||||
if end != -1 {
|
||||
t.Fatal(end)
|
||||
}
|
||||
@ -91,54 +91,54 @@ func (reader *StagedReader) Read(p []byte) (n int, err error) {
|
||||
|
||||
func Test_skip_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`"abc`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `"abc`)
|
||||
iter.skipString()
|
||||
should.Equal(1, iter.head)
|
||||
iter = ParseString(`\""abc`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `\""abc`)
|
||||
iter.skipString()
|
||||
should.Equal(3, iter.head)
|
||||
reader := &StagedReader{
|
||||
r1: `abc`,
|
||||
r2: `"`,
|
||||
}
|
||||
iter = Parse(reader, 4096)
|
||||
iter = Parse(DEFAULT_CONFIG, reader, 4096)
|
||||
iter.skipString()
|
||||
should.Equal(1, iter.head)
|
||||
reader = &StagedReader{
|
||||
r1: `abc`,
|
||||
r2: `1"`,
|
||||
}
|
||||
iter = Parse(reader, 4096)
|
||||
iter = Parse(DEFAULT_CONFIG, reader, 4096)
|
||||
iter.skipString()
|
||||
should.Equal(2, iter.head)
|
||||
reader = &StagedReader{
|
||||
r1: `abc\`,
|
||||
r2: `"`,
|
||||
}
|
||||
iter = Parse(reader, 4096)
|
||||
iter = Parse(DEFAULT_CONFIG, reader, 4096)
|
||||
iter.skipString()
|
||||
should.NotNil(iter.Error)
|
||||
reader = &StagedReader{
|
||||
r1: `abc\`,
|
||||
r2: `""`,
|
||||
}
|
||||
iter = Parse(reader, 4096)
|
||||
iter = Parse(DEFAULT_CONFIG, reader, 4096)
|
||||
iter.skipString()
|
||||
should.Equal(2, iter.head)
|
||||
}
|
||||
|
||||
func Test_skip_object(t *testing.T) {
|
||||
iter := ParseString(`}`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `}`)
|
||||
iter.skipObject()
|
||||
if iter.head != 1 {
|
||||
t.Fatal(iter.head)
|
||||
}
|
||||
iter = ParseString(`a}`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `a}`)
|
||||
iter.skipObject()
|
||||
if iter.head != 2 {
|
||||
t.Fatal(iter.head)
|
||||
}
|
||||
iter = ParseString(`{}}a`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `{}}a`)
|
||||
iter.skipObject()
|
||||
if iter.head != 3 {
|
||||
t.Fatal(iter.head)
|
||||
@ -147,12 +147,12 @@ func Test_skip_object(t *testing.T) {
|
||||
r1: `{`,
|
||||
r2: `}}a`,
|
||||
}
|
||||
iter = Parse(reader, 4096)
|
||||
iter = Parse(DEFAULT_CONFIG, reader, 4096)
|
||||
iter.skipObject()
|
||||
if iter.head != 2 {
|
||||
t.Fatal(iter.head)
|
||||
}
|
||||
iter = ParseString(`"}"}a`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `"}"}a`)
|
||||
iter.skipObject()
|
||||
if iter.head != 4 {
|
||||
t.Fatal(iter.head)
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
|
||||
func Test_read_big_float(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`12.3`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `12.3`)
|
||||
val := iter.ReadBigFloat()
|
||||
val64, _ := val.Float64()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`92233720368547758079223372036854775807`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `92233720368547758079223372036854775807`)
|
||||
val := iter.ReadBigInt()
|
||||
should.NotNil(val)
|
||||
should.Equal(`92233720368547758079223372036854775807`, val.String())
|
||||
@ -31,14 +31,14 @@ func Test_read_float(t *testing.T) {
|
||||
// non-streaming
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(input + ",")
|
||||
iter := ParseString(DEFAULT_CONFIG, input+",")
|
||||
expected, err := strconv.ParseFloat(input, 32)
|
||||
should.Nil(err)
|
||||
should.Equal(float32(expected), iter.ReadFloat32())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(input + ",")
|
||||
iter := ParseString(DEFAULT_CONFIG, input+",")
|
||||
expected, err := strconv.ParseFloat(input, 64)
|
||||
should.Nil(err)
|
||||
should.Equal(expected, iter.ReadFloat64())
|
||||
@ -46,14 +46,14 @@ func Test_read_float(t *testing.T) {
|
||||
// streaming
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(bytes.NewBufferString(input+","), 2)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input+","), 2)
|
||||
expected, err := strconv.ParseFloat(input, 32)
|
||||
should.Nil(err)
|
||||
should.Equal(float32(expected), iter.ReadFloat32())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(bytes.NewBufferString(input+","), 2)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input+","), 2)
|
||||
expected, err := strconv.ParseFloat(input, 64)
|
||||
should.Nil(err)
|
||||
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) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`12.3`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `12.3`)
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteFloat32Lossy(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -108,7 +108,7 @@ func Test_write_float32(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 10)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 10)
|
||||
stream.WriteRaw("abcdefg")
|
||||
stream.WriteFloat32Lossy(1.123456)
|
||||
stream.Flush()
|
||||
@ -123,7 +123,7 @@ func Test_write_float64(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteFloat64Lossy(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -141,7 +141,7 @@ func Test_write_float64(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 10)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 10)
|
||||
stream.WriteRaw("abcdefg")
|
||||
stream.WriteFloat64Lossy(1.123456)
|
||||
stream.Flush()
|
||||
@ -151,7 +151,7 @@ func Test_write_float64(t *testing.T) {
|
||||
|
||||
func Test_read_float64_cursor(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString("[1.23456789\n,2,3]")
|
||||
iter := ParseString(DEFAULT_CONFIG, "[1.23456789\n,2,3]")
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal(1.23456789, iter.Read())
|
||||
should.True(iter.ReadArray())
|
||||
@ -174,7 +174,7 @@ func Test_read_float_scientific(t *testing.T) {
|
||||
func Benchmark_jsoniter_float(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
input := []byte(`1.1123,`)
|
||||
iter := NewIterator()
|
||||
iter := NewIterator(DEFAULT_CONFIG)
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter.ResetBytes(input)
|
||||
iter.ReadFloat64()
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
|
||||
func Test_read_uint64_invalid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(",")
|
||||
iter := ParseString(DEFAULT_CONFIG, ",")
|
||||
iter.ReadUint64()
|
||||
should.NotNil(iter.Error)
|
||||
}
|
||||
@ -23,7 +23,7 @@ func Test_read_int8(t *testing.T) {
|
||||
for _, input := range inputs {
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(input)
|
||||
iter := ParseString(DEFAULT_CONFIG, input)
|
||||
expected, err := strconv.ParseInt(input, 10, 8)
|
||||
should.Nil(err)
|
||||
should.Equal(int8(expected), iter.ReadInt8())
|
||||
@ -36,7 +36,7 @@ func Test_read_int16(t *testing.T) {
|
||||
for _, input := range inputs {
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(input)
|
||||
iter := ParseString(DEFAULT_CONFIG, input)
|
||||
expected, err := strconv.ParseInt(input, 10, 16)
|
||||
should.Nil(err)
|
||||
should.Equal(int16(expected), iter.ReadInt16())
|
||||
@ -49,14 +49,14 @@ func Test_read_int32(t *testing.T) {
|
||||
for _, input := range inputs {
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(input)
|
||||
iter := ParseString(DEFAULT_CONFIG, input)
|
||||
expected, err := strconv.ParseInt(input, 10, 32)
|
||||
should.Nil(err)
|
||||
should.Equal(int32(expected), iter.ReadInt32())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(bytes.NewBufferString(input), 2)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input), 2)
|
||||
expected, err := strconv.ParseInt(input, 10, 32)
|
||||
should.Nil(err)
|
||||
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) {
|
||||
should := require.New(t)
|
||||
input := "123456789123456789,"
|
||||
iter := ParseString(input)
|
||||
iter := ParseString(DEFAULT_CONFIG, input)
|
||||
iter.ReadInt32()
|
||||
should.NotNil(iter.Error)
|
||||
}
|
||||
@ -93,14 +93,14 @@ func Test_read_int64(t *testing.T) {
|
||||
for _, input := range inputs {
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(input)
|
||||
iter := ParseString(DEFAULT_CONFIG, input)
|
||||
expected, err := strconv.ParseInt(input, 10, 64)
|
||||
should.Nil(err)
|
||||
should.Equal(expected, iter.ReadInt64())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(bytes.NewBufferString(input), 2)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input), 2)
|
||||
expected, err := strconv.ParseInt(input, 10, 64)
|
||||
should.Nil(err)
|
||||
should.Equal(expected, iter.ReadInt64())
|
||||
@ -111,7 +111,7 @@ func Test_read_int64(t *testing.T) {
|
||||
func Test_read_int64_overflow(t *testing.T) {
|
||||
should := require.New(t)
|
||||
input := "123456789123456789123456789123456789,"
|
||||
iter := ParseString(input)
|
||||
iter := ParseString(DEFAULT_CONFIG, input)
|
||||
iter.ReadInt64()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteUint8(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -164,7 +164,7 @@ func Test_write_uint8(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 3)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 3)
|
||||
stream.WriteRaw("a")
|
||||
stream.WriteUint8(100) // should clear buffer
|
||||
stream.Flush()
|
||||
@ -178,7 +178,7 @@ func Test_write_int8(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteInt8(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -196,7 +196,7 @@ func Test_write_int8(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4)
|
||||
stream.WriteRaw("a")
|
||||
stream.WriteInt8(-100) // should clear buffer
|
||||
stream.Flush()
|
||||
@ -210,7 +210,7 @@ func Test_write_uint16(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteUint16(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -228,7 +228,7 @@ func Test_write_uint16(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 5)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 5)
|
||||
stream.WriteRaw("a")
|
||||
stream.WriteUint16(10000) // should clear buffer
|
||||
stream.Flush()
|
||||
@ -242,7 +242,7 @@ func Test_write_int16(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteInt16(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -260,7 +260,7 @@ func Test_write_int16(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 6)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 6)
|
||||
stream.WriteRaw("a")
|
||||
stream.WriteInt16(-10000) // should clear buffer
|
||||
stream.Flush()
|
||||
@ -274,7 +274,7 @@ func Test_write_uint32(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteUint32(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -292,7 +292,7 @@ func Test_write_uint32(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 10)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 10)
|
||||
stream.WriteRaw("a")
|
||||
stream.WriteUint32(0xffffffff) // should clear buffer
|
||||
stream.Flush()
|
||||
@ -306,7 +306,7 @@ func Test_write_int32(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteInt32(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -324,7 +324,7 @@ func Test_write_int32(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 11)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 11)
|
||||
stream.WriteRaw("a")
|
||||
stream.WriteInt32(-0x7fffffff) // should clear buffer
|
||||
stream.Flush()
|
||||
@ -340,7 +340,7 @@ func Test_write_uint64(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteUint64(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -358,7 +358,7 @@ func Test_write_uint64(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 10)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 10)
|
||||
stream.WriteRaw("a")
|
||||
stream.WriteUint64(0xffffffff) // should clear buffer
|
||||
stream.Flush()
|
||||
@ -374,7 +374,7 @@ func Test_write_int64(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteInt64(val)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(val)
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -392,7 +392,7 @@ func Test_write_int64(t *testing.T) {
|
||||
}
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 10)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 10)
|
||||
stream.WriteRaw("a")
|
||||
stream.WriteInt64(0xffffffff) // should clear buffer
|
||||
stream.Flush()
|
||||
@ -403,7 +403,7 @@ func Test_write_int64(t *testing.T) {
|
||||
func Test_write_val_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal(1001)
|
||||
stream.Flush()
|
||||
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) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
val := 1001
|
||||
stream.WriteVal(&val)
|
||||
stream.Flush()
|
||||
@ -433,7 +433,7 @@ func Test_json_number(t *testing.T) {
|
||||
}
|
||||
|
||||
func Benchmark_jsoniter_encode_int(b *testing.B) {
|
||||
stream := NewStream(ioutil.Discard, 64)
|
||||
stream := NewStream(DEFAULT_CONFIG, ioutil.Discard, 64)
|
||||
for n := 0; n < b.N; n++ {
|
||||
stream.n = 0
|
||||
stream.WriteUint64(0xffffffff)
|
||||
@ -447,7 +447,7 @@ func Benchmark_itoa(b *testing.B) {
|
||||
}
|
||||
|
||||
func Benchmark_jsoniter_int(b *testing.B) {
|
||||
iter := NewIterator()
|
||||
iter := NewIterator(DEFAULT_CONFIG)
|
||||
input := []byte(`100`)
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter.ResetBytes(input)
|
||||
|
@ -1,10 +1,10 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
"unsafe"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
func Test_nil_non_empty_interface(t *testing.T) {
|
||||
CleanEncoders()
|
||||
CleanDecoders()
|
||||
DEFAULT_CONFIG.CleanEncoders()
|
||||
DEFAULT_CONFIG.CleanDecoders()
|
||||
type TestObject struct {
|
||||
Field []MyInterface
|
||||
}
|
||||
@ -152,4 +151,4 @@ func Test_nil_non_empty_interface(t *testing.T) {
|
||||
b := []byte(`{"Field":["AAA"]}`)
|
||||
should.NotNil(json.Unmarshal(b, &obj))
|
||||
should.NotNil(Unmarshal(b, &obj))
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func Test_read_by_one(t *testing.T) {
|
||||
iter := Parse(bytes.NewBufferString("abc"), 1)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString("abc"), 1)
|
||||
b := iter.readByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
@ -34,7 +34,7 @@ func Test_read_by_one(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_read_by_two(t *testing.T) {
|
||||
iter := Parse(bytes.NewBufferString("abc"), 2)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString("abc"), 2)
|
||||
b := iter.readByte()
|
||||
if iter.Error != nil {
|
||||
t.Fatal(iter.Error)
|
||||
@ -67,7 +67,7 @@ func Test_read_by_two(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_read_until_eof(t *testing.T) {
|
||||
iter := Parse(bytes.NewBufferString("abc"), 2)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString("abc"), 2)
|
||||
iter.readByte()
|
||||
iter.readByte()
|
||||
b := iter.readByte()
|
||||
|
@ -27,7 +27,7 @@ func Benchmark_jsoniter_large_file(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for n := 0; n < b.N; n++ {
|
||||
file, _ := os.Open("/tmp/large-file.json")
|
||||
iter := Parse(file, 4096)
|
||||
iter := Parse(DEFAULT_CONFIG, file, 4096)
|
||||
count := 0
|
||||
for iter.ReadArray() {
|
||||
iter.Skip()
|
||||
|
@ -1,15 +1,15 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"math/big"
|
||||
"testing"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func Test_read_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`{"hello": "world"}`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `{"hello": "world"}`)
|
||||
m := map[string]string{"1": "2"}
|
||||
iter.ReadVal(&m)
|
||||
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) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`{"hello": "world"}`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `{"hello": "world"}`)
|
||||
m := map[string]interface{}{"1": "2"}
|
||||
iter.ReadVal(&m)
|
||||
should.Equal(map[string]interface{}{"1": "2", "hello": "world"}, m)
|
||||
iter = ParseString(`{"hello": "world"}`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `{"hello": "world"}`)
|
||||
should.Equal(map[string]interface{}{"hello": "world"}, iter.Read())
|
||||
}
|
||||
|
||||
@ -117,11 +117,11 @@ func Test_map_key_with_escaped_char(t *testing.T) {
|
||||
{
|
||||
var obj Ttest
|
||||
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
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ type Level2 struct {
|
||||
}
|
||||
|
||||
func Test_nested(t *testing.T) {
|
||||
iter := ParseString(`{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||
l1 := Level1{}
|
||||
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
||||
switch l1Field {
|
||||
@ -50,7 +50,7 @@ func Test_nested(t *testing.T) {
|
||||
|
||||
func Benchmark_jsoniter_nested(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter := ParseString(`{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `{"hello": [{"world": "value1"}, {"world": "value2"}]}`)
|
||||
l1 := Level1{}
|
||||
for l1Field := iter.ReadObject(); l1Field != ""; l1Field = iter.ReadObject() {
|
||||
switch l1Field {
|
||||
|
@ -2,18 +2,18 @@ package jsoniter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/json-iterator/go/require"
|
||||
"testing"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func Test_read_null(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`null`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `null`)
|
||||
should.True(iter.ReadNil())
|
||||
iter = ParseString(`null`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `null`)
|
||||
should.Nil(iter.Read())
|
||||
iter = ParseString(`null`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `null`)
|
||||
any, err := UnmarshalAnyFromString(`null`)
|
||||
should.Nil(err)
|
||||
should.Equal(0, any.ToInt())
|
||||
@ -25,7 +25,7 @@ func Test_read_null(t *testing.T) {
|
||||
func Test_write_null(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteNil()
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -41,7 +41,7 @@ func Test_encode_null(t *testing.T) {
|
||||
|
||||
func Test_decode_null_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`[null,"a"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[null,"a"]`)
|
||||
iter.ReadArray()
|
||||
if iter.ReadObject() != "" {
|
||||
t.FailNow()
|
||||
@ -59,7 +59,7 @@ func Test_decode_null_object(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_decode_null_array(t *testing.T) {
|
||||
iter := ParseString(`[null,"a"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[null,"a"]`)
|
||||
iter.ReadArray()
|
||||
if iter.ReadArray() != false {
|
||||
t.FailNow()
|
||||
@ -72,7 +72,7 @@ func Test_decode_null_array(t *testing.T) {
|
||||
|
||||
func Test_decode_null_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`[null,"a"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[null,"a"]`)
|
||||
should.True(iter.ReadArray())
|
||||
should.Equal("", iter.ReadString())
|
||||
should.True(iter.ReadArray())
|
||||
@ -80,7 +80,7 @@ func Test_decode_null_string(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_decode_null_skip(t *testing.T) {
|
||||
iter := ParseString(`[null,"a"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[null,"a"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
|
@ -9,10 +9,10 @@ import (
|
||||
|
||||
func Test_empty_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`{}`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `{}`)
|
||||
field := iter.ReadObject()
|
||||
should.Equal("", field)
|
||||
iter = ParseString(`{}`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `{}`)
|
||||
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||
should.FailNow("should not call")
|
||||
return true
|
||||
@ -21,14 +21,14 @@ func Test_empty_object(t *testing.T) {
|
||||
|
||||
func Test_one_field(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`{"a": "b"}`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `{"a": "b"}`)
|
||||
field := iter.ReadObject()
|
||||
should.Equal("a", field)
|
||||
value := iter.ReadString()
|
||||
should.Equal("b", value)
|
||||
field = iter.ReadObject()
|
||||
should.Equal("", field)
|
||||
iter = ParseString(`{"a": "b"}`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `{"a": "b"}`)
|
||||
should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
||||
should.Equal("a", field)
|
||||
return true
|
||||
@ -37,7 +37,7 @@ func Test_one_field(t *testing.T) {
|
||||
|
||||
func Test_two_field(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`{ "a": "b" , "c": "d" }`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `{ "a": "b" , "c": "d" }`)
|
||||
field := iter.ReadObject()
|
||||
should.Equal("a", field)
|
||||
value := iter.ReadString()
|
||||
@ -48,7 +48,7 @@ func Test_two_field(t *testing.T) {
|
||||
should.Equal("d", value)
|
||||
field = iter.ReadObject()
|
||||
should.Equal("", field)
|
||||
iter = ParseString(`{"field1": "1", "field2": 2}`)
|
||||
iter = ParseString(DEFAULT_CONFIG, `{"field1": "1", "field2": 2}`)
|
||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||
switch field {
|
||||
case "field1":
|
||||
@ -210,7 +210,7 @@ func Test_object_wrapper_any_get_all(t *testing.T) {
|
||||
func Test_write_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.IndentionStep = 2
|
||||
stream.WriteObjectStart()
|
||||
stream.WriteObjectField("hello")
|
||||
@ -230,7 +230,7 @@ func Benchmark_jsoniter_object(b *testing.B) {
|
||||
Field2 uint64
|
||||
}
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter := ParseString(`{"field1": "1", "field2": 2}`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `{"field1": "1", "field2": 2}`)
|
||||
obj := TestObj{}
|
||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||
switch field {
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func Test_reflect_str(t *testing.T) {
|
||||
iter := ParseString(`"hello"`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `"hello"`)
|
||||
str := ""
|
||||
iter.ReadVal(&str)
|
||||
if str != "hello" {
|
||||
@ -16,7 +16,7 @@ func Test_reflect_str(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_ptr_str(t *testing.T) {
|
||||
iter := ParseString(`"hello"`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `"hello"`)
|
||||
var str *string
|
||||
iter.ReadVal(&str)
|
||||
if *str != "hello" {
|
||||
@ -25,7 +25,7 @@ func Test_reflect_ptr_str(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_int(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := int(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -34,7 +34,7 @@ func Test_reflect_int(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_int8(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := int8(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -43,7 +43,7 @@ func Test_reflect_int8(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_int16(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := int16(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -52,7 +52,7 @@ func Test_reflect_int16(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_int32(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := int32(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -61,7 +61,7 @@ func Test_reflect_int32(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_int64(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := int64(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -70,7 +70,7 @@ func Test_reflect_int64(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_uint(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := uint(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -79,7 +79,7 @@ func Test_reflect_uint(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_uint8(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := uint8(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -88,7 +88,7 @@ func Test_reflect_uint8(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_uint16(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := uint16(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -97,7 +97,7 @@ func Test_reflect_uint16(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_uint32(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := uint32(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -106,7 +106,7 @@ func Test_reflect_uint32(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_uint64(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := uint64(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -115,7 +115,7 @@ func Test_reflect_uint64(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_byte(t *testing.T) {
|
||||
iter := ParseString(`123`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `123`)
|
||||
val := byte(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 123 {
|
||||
@ -124,7 +124,7 @@ func Test_reflect_byte(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_float32(t *testing.T) {
|
||||
iter := ParseString(`1.23`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `1.23`)
|
||||
val := float32(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 1.23 {
|
||||
@ -134,7 +134,7 @@ func Test_reflect_float32(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_float64(t *testing.T) {
|
||||
iter := ParseString(`1.23`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `1.23`)
|
||||
val := float64(0)
|
||||
iter.ReadVal(&val)
|
||||
if val != 1.23 {
|
||||
@ -144,7 +144,7 @@ func Test_reflect_float64(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_reflect_bool(t *testing.T) {
|
||||
iter := ParseString(`true`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `true`)
|
||||
val := false
|
||||
iter.ReadVal(&val)
|
||||
if val != true {
|
||||
|
@ -27,7 +27,7 @@ func Test_decode_nested(t *testing.T) {
|
||||
field1 string
|
||||
field2 string
|
||||
}
|
||||
iter := ParseString(`[{"field1": "hello"}, null, {"field2": "world"}]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[{"field1": "hello"}, null, {"field2": "world"}]`)
|
||||
slice := []*StructOfString{}
|
||||
iter.ReadVal(&slice)
|
||||
if len(slice) != 3 {
|
||||
@ -49,12 +49,12 @@ func Test_decode_nested(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_decode_base64(t *testing.T) {
|
||||
iter := ParseString(`"YWJj"`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `"YWJj"`)
|
||||
val := []byte{}
|
||||
RegisterTypeDecoder("[]uint8", func(ptr unsafe.Pointer, iter *Iterator) {
|
||||
*((*[]byte)(ptr)) = iter.ReadBase64()
|
||||
})
|
||||
defer CleanDecoders()
|
||||
defer DEFAULT_CONFIG.CleanDecoders()
|
||||
iter.ReadVal(&val)
|
||||
if "abc" != string(val) {
|
||||
t.Fatal(string(val))
|
||||
@ -70,7 +70,7 @@ type StructOfTagOne struct {
|
||||
|
||||
func Benchmark_jsoniter_reflect(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
iter := NewIterator()
|
||||
iter := NewIterator(DEFAULT_CONFIG)
|
||||
Struct := &StructOfTagOne{}
|
||||
//var Struct *StructOfTagOne
|
||||
input := []byte(`{"field3": "100", "field4": "100"}`)
|
||||
@ -96,7 +96,7 @@ func Benchmark_jsoniter_direct(b *testing.B) {
|
||||
// iter.Skip()
|
||||
// }
|
||||
//}
|
||||
iter := ParseString(`["hello", "world"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `["hello", "world"]`)
|
||||
array := make([]string, 0, 2)
|
||||
for iter.ReadArray() {
|
||||
array = append(array, iter.ReadString())
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func Test_skip_number(t *testing.T) {
|
||||
iter := ParseString(`[-0.12, "b"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[-0.12, "b"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
@ -16,7 +16,7 @@ func Test_skip_number(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_skip_null(t *testing.T) {
|
||||
iter := ParseString(`[null , "b"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[null , "b"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
@ -26,7 +26,7 @@ func Test_skip_null(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_skip_true(t *testing.T) {
|
||||
iter := ParseString(`[true , "b"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[true , "b"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
@ -36,7 +36,7 @@ func Test_skip_true(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_skip_false(t *testing.T) {
|
||||
iter := ParseString(`[false , "b"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[false , "b"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
@ -46,7 +46,7 @@ func Test_skip_false(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_skip_array(t *testing.T) {
|
||||
iter := ParseString(`[[1, [2, [3], 4]], "b"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[[1, [2, [3], 4]], "b"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
@ -56,7 +56,7 @@ func Test_skip_array(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_skip_empty_array(t *testing.T) {
|
||||
iter := ParseString(`[ [ ], "b"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[ [ ], "b"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
@ -66,7 +66,7 @@ func Test_skip_empty_array(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_skip_nested(t *testing.T) {
|
||||
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
|
||||
iter.ReadArray()
|
||||
iter.Skip()
|
||||
iter.ReadArray()
|
||||
@ -106,7 +106,7 @@ func Benchmark_jsoniter_skip(b *testing.B) {
|
||||
}`)
|
||||
for n := 0; n < b.N; n++ {
|
||||
result := TestResp{}
|
||||
iter := ParseBytes(input)
|
||||
iter := ParseBytes(DEFAULT_CONFIG, input)
|
||||
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
|
||||
switch field {
|
||||
case "code":
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
|
||||
func Test_writeByte_should_grow_buffer(t *testing.T) {
|
||||
should := require.New(t)
|
||||
stream := NewStream(nil, 1)
|
||||
stream := NewStream(DEFAULT_CONFIG, nil, 1)
|
||||
stream.writeByte('1')
|
||||
should.Equal("1", string(stream.Buffer()))
|
||||
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) {
|
||||
should := require.New(t)
|
||||
stream := NewStream(nil, 1)
|
||||
stream := NewStream(DEFAULT_CONFIG, nil, 1)
|
||||
stream.Write([]byte{'1', '2'})
|
||||
should.Equal("12", string(stream.Buffer()))
|
||||
should.Equal(3, len(stream.buf))
|
||||
@ -31,7 +31,7 @@ func Test_writeBytes_should_grow_buffer(t *testing.T) {
|
||||
|
||||
func Test_writeIndention_should_grow_buffer(t *testing.T) {
|
||||
should := require.New(t)
|
||||
stream := NewStream(nil, 1)
|
||||
stream := NewStream(DEFAULT_CONFIG, nil, 1)
|
||||
stream.IndentionStep = 2
|
||||
stream.WriteVal([]int{1, 2, 3})
|
||||
should.Equal("[\n 1,\n 2,\n 3\n]", string(stream.Buffer()))
|
||||
@ -39,7 +39,7 @@ func Test_writeIndention_should_grow_buffer(t *testing.T) {
|
||||
|
||||
func Test_writeRaw_should_grow_buffer(t *testing.T) {
|
||||
should := require.New(t)
|
||||
stream := NewStream(nil, 1)
|
||||
stream := NewStream(DEFAULT_CONFIG, nil, 1)
|
||||
stream.WriteRaw("123")
|
||||
should.Nil(stream.Error)
|
||||
should.Equal("123", string(stream.Buffer()))
|
||||
@ -47,7 +47,7 @@ func Test_writeRaw_should_grow_buffer(t *testing.T) {
|
||||
|
||||
func Test_writeString_should_grow_buffer(t *testing.T) {
|
||||
should := require.New(t)
|
||||
stream := NewStream(nil, 0)
|
||||
stream := NewStream(DEFAULT_CONFIG, nil, 0)
|
||||
stream.WriteString("123")
|
||||
should.Nil(stream.Error)
|
||||
should.Equal(`"123"`, string(stream.Buffer()))
|
||||
|
@ -17,22 +17,22 @@ func Test_read_normal_string(t *testing.T) {
|
||||
for input, output := range cases {
|
||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(input)
|
||||
iter := ParseString(DEFAULT_CONFIG, input)
|
||||
should.Equal(output, iter.ReadString())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(bytes.NewBufferString(input), 2)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input), 2)
|
||||
should.Equal(output, iter.ReadString())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(input)
|
||||
iter := ParseString(DEFAULT_CONFIG, input)
|
||||
should.Equal(output, string(iter.ReadStringAsSlice()))
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(bytes.NewBufferString(input), 2)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input), 2)
|
||||
should.Equal(output, string(iter.ReadStringAsSlice()))
|
||||
})
|
||||
}
|
||||
@ -48,12 +48,12 @@ func Test_read_exotic_string(t *testing.T) {
|
||||
for input, output := range cases {
|
||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(input)
|
||||
iter := ParseString(DEFAULT_CONFIG, input)
|
||||
should.Equal(output, iter.ReadString())
|
||||
})
|
||||
t.Run(fmt.Sprintf("%v:%v", input, output), func(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := Parse(bytes.NewBufferString(input), 2)
|
||||
iter := Parse(DEFAULT_CONFIG, bytes.NewBufferString(input), 2)
|
||||
should.Equal(output, iter.ReadString())
|
||||
})
|
||||
}
|
||||
@ -61,7 +61,7 @@ func Test_read_exotic_string(t *testing.T) {
|
||||
|
||||
func Test_read_string_as_interface(t *testing.T) {
|
||||
should := require.New(t)
|
||||
iter := ParseString(`"hello"`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `"hello"`)
|
||||
should.Equal("hello", iter.Read())
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ func Test_write_string(t *testing.T) {
|
||||
func Test_write_val_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
buf := &bytes.Buffer{}
|
||||
stream := NewStream(buf, 4096)
|
||||
stream := NewStream(DEFAULT_CONFIG, buf, 4096)
|
||||
stream.WriteVal("hello")
|
||||
stream.Flush()
|
||||
should.Nil(stream.Error)
|
||||
@ -124,13 +124,13 @@ func Test_decode_slash(t *testing.T) {
|
||||
|
||||
func Benchmark_jsoniter_unicode(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter := ParseString(`"\ud83d\udc4a"`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `"\ud83d\udc4a"`)
|
||||
iter.ReadString()
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_jsoniter_ascii(b *testing.B) {
|
||||
iter := NewIterator()
|
||||
iter := NewIterator(DEFAULT_CONFIG)
|
||||
input := []byte(`"hello, world! hello, world!"`)
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
@ -140,7 +140,7 @@ func Benchmark_jsoniter_ascii(b *testing.B) {
|
||||
}
|
||||
|
||||
func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
|
||||
iter := ParseString(`"hello, world!"`)
|
||||
iter := ParseString(DEFAULT_CONFIG, `"hello, world!"`)
|
||||
b.ResetTimer()
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter.ResetBytes(iter.buf)
|
||||
|
Loading…
x
Reference in New Issue
Block a user