1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00

rename Encoder => ValEncoder and Decoder => ValDecoder

This commit is contained in:
Tao Wen 2017-06-20 07:59:45 +08:00
parent 43a832beee
commit b3170a8cef
7 changed files with 68 additions and 68 deletions

View File

@ -60,8 +60,8 @@ func (cfg Config) Froze() *frozenConfig {
streamPool: make(chan *Stream, 16),
iteratorPool: make(chan *Iterator, 16),
}
atomic.StorePointer(&frozenConfig.decoderCache, unsafe.Pointer(&map[string]Decoder{}))
atomic.StorePointer(&frozenConfig.encoderCache, unsafe.Pointer(&map[string]Encoder{}))
atomic.StorePointer(&frozenConfig.decoderCache, unsafe.Pointer(&map[string]ValDecoder{}))
atomic.StorePointer(&frozenConfig.encoderCache, unsafe.Pointer(&map[string]ValEncoder{}))
if cfg.MarshalFloatWith6Digits {
frozenConfig.marshalFloatWith6Digits()
}
@ -158,12 +158,12 @@ func (cfg *frozenConfig) escapeHtml() {
cfg.addEncoderToCache(reflect.TypeOf((*string)(nil)).Elem(), &htmlEscapedStringEncoder{})
}
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder Decoder) {
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder ValDecoder) {
done := false
for !done {
ptr := atomic.LoadPointer(&cfg.decoderCache)
cache := *(*map[reflect.Type]Decoder)(ptr)
copied := map[reflect.Type]Decoder{}
cache := *(*map[reflect.Type]ValDecoder)(ptr)
copied := map[reflect.Type]ValDecoder{}
for k, v := range cache {
copied[k] = v
}
@ -172,12 +172,12 @@ func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder Decode
}
}
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder Encoder) {
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder ValEncoder) {
done := false
for !done {
ptr := atomic.LoadPointer(&cfg.encoderCache)
cache := *(*map[reflect.Type]Encoder)(ptr)
copied := map[reflect.Type]Encoder{}
cache := *(*map[reflect.Type]ValEncoder)(ptr)
copied := map[reflect.Type]ValEncoder{}
for k, v := range cache {
copied[k] = v
}
@ -186,30 +186,30 @@ func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder Encode
}
}
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) Decoder {
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) ValDecoder {
ptr := atomic.LoadPointer(&cfg.decoderCache)
cache := *(*map[reflect.Type]Decoder)(ptr)
cache := *(*map[reflect.Type]ValDecoder)(ptr)
return cache[cacheKey]
}
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) Encoder {
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
ptr := atomic.LoadPointer(&cfg.encoderCache)
cache := *(*map[reflect.Type]Encoder)(ptr)
cache := *(*map[reflect.Type]ValEncoder)(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{}))
typeDecoders = map[string]ValDecoder{}
fieldDecoders = map[string]ValDecoder{}
atomic.StorePointer(&cfg.decoderCache, unsafe.Pointer(&map[string]ValDecoder{}))
}
// 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{}))
typeEncoders = map[string]ValEncoder{}
fieldEncoders = map[string]ValEncoder{}
atomic.StorePointer(&cfg.encoderCache, unsafe.Pointer(&map[string]ValEncoder{}))
}
func (cfg *frozenConfig) MarshalToString(v interface{}) (string, error) {

View File

@ -9,8 +9,8 @@ import (
"unsafe"
)
// Decoder is an internal type registered to cache as needed.
// Don't confuse jsoniter.Decoder with json.Decoder.
// ValDecoder is an internal type registered to cache as needed.
// Don't confuse jsoniter.ValDecoder with json.Decoder.
// For json.Decoder's adapter, refer to jsoniter.AdapterDecoder(todo link).
//
// Reflection on type to create decoders, which is then cached
@ -19,20 +19,20 @@ import (
// 2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New
// 3. assignment to map, both key and value will be reflect.Value
// For a simple struct binding, it will be reflect.Value free and allocation free
type Decoder interface {
type ValDecoder interface {
decode(ptr unsafe.Pointer, iter *Iterator)
}
// Encoder is an internal type registered to cache as needed.
// Don't confuse jsoniter.Encoder with json.Encoder.
// ValEncoder is an internal type registered to cache as needed.
// Don't confuse jsoniter.ValEncoder with json.Encoder.
// For json.Encoder's adapter, refer to jsoniter.AdapterEncoder(todo godoc link).
type Encoder interface {
type ValEncoder interface {
isEmpty(ptr unsafe.Pointer) bool
encode(ptr unsafe.Pointer, stream *Stream)
encodeInterface(val interface{}, stream *Stream)
}
func writeToStream(val interface{}, stream *Stream, encoder Encoder) {
func writeToStream(val interface{}, stream *Stream, encoder ValEncoder) {
e := (*emptyInterface)(unsafe.Pointer(&val))
if e.word == nil {
stream.WriteNil()
@ -58,7 +58,7 @@ func (decoder *funcDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
}
type funcEncoder struct {
fun EncoderFunc
fun EncoderFunc
isEmptyFunc func(ptr unsafe.Pointer) bool
}
@ -77,10 +77,10 @@ func (encoder *funcEncoder) isEmpty(ptr unsafe.Pointer) bool {
return encoder.isEmptyFunc(ptr)
}
var typeDecoders map[string]Decoder
var fieldDecoders map[string]Decoder
var typeEncoders map[string]Encoder
var fieldEncoders map[string]Encoder
var typeDecoders map[string]ValDecoder
var fieldDecoders map[string]ValDecoder
var typeEncoders map[string]ValEncoder
var fieldEncoders map[string]ValEncoder
var extensions []ExtensionFunc
var jsonNumberType reflect.Type
var jsonRawMessageType reflect.Type
@ -91,10 +91,10 @@ var unmarshalerType reflect.Type
var textUnmarshalerType reflect.Type
func init() {
typeDecoders = map[string]Decoder{}
fieldDecoders = map[string]Decoder{}
typeEncoders = map[string]Encoder{}
fieldEncoders = map[string]Encoder{}
typeDecoders = map[string]ValDecoder{}
fieldDecoders = map[string]ValDecoder{}
typeEncoders = map[string]ValEncoder{}
fieldEncoders = map[string]ValEncoder{}
extensions = []ExtensionFunc{}
jsonNumberType = reflect.TypeOf((*json.Number)(nil)).Elem()
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
@ -130,7 +130,7 @@ func RegisterExtension(extension ExtensionFunc) {
type optionalDecoder struct {
valueType reflect.Type
valueDecoder Decoder
valueDecoder ValDecoder
}
func (decoder *optionalDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
@ -150,7 +150,7 @@ func (decoder *optionalDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
}
type optionalEncoder struct {
valueEncoder Encoder
valueEncoder ValEncoder
}
func (encoder *optionalEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
@ -190,7 +190,7 @@ func (encoder *placeholderEncoder) isEmpty(ptr unsafe.Pointer) bool {
return encoder.getRealEncoder().isEmpty(ptr)
}
func (encoder *placeholderEncoder) getRealEncoder() Encoder {
func (encoder *placeholderEncoder) getRealEncoder() ValEncoder {
for i := 0; i < 30; i++ {
realDecoder := encoder.cfg.getEncoderFromCache(encoder.cacheKey)
_, isPlaceholder := realDecoder.(*placeholderEncoder)
@ -282,21 +282,21 @@ func (stream *Stream) WriteVal(val interface{}) {
type prefix string
func (p prefix) addToDecoder(decoder Decoder, err error) (Decoder, error) {
func (p prefix) addToDecoder(decoder ValDecoder, err error) (ValDecoder, error) {
if err != nil {
return nil, fmt.Errorf("%s: %s", p, err.Error())
}
return decoder, err
}
func (p prefix) addToEncoder(encoder Encoder, err error) (Encoder, error) {
func (p prefix) addToEncoder(encoder ValEncoder, err error) (ValEncoder, error) {
if err != nil {
return nil, fmt.Errorf("%s: %s", p, err.Error())
}
return encoder, err
}
func decoderOfType(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
func decoderOfType(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
typeName := typ.String()
typeDecoder := typeDecoders[typeName]
if typeDecoder != nil {
@ -320,7 +320,7 @@ func decoderOfType(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
return newDecoder, err
}
func createDecoderOfType(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
func createDecoderOfType(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
if typ.String() == "[]uint8" {
return &base64Codec{}, nil
}
@ -335,7 +335,7 @@ func createDecoderOfType(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
}
if typ.ConvertibleTo(unmarshalerType) {
templateInterface := reflect.New(typ).Elem().Interface()
var decoder Decoder = &unmarshalerDecoder{extractInterface(templateInterface)}
var decoder ValDecoder = &unmarshalerDecoder{extractInterface(templateInterface)}
if typ.Kind() != reflect.Struct {
decoder = &optionalDecoder{typ, decoder}
}
@ -394,7 +394,7 @@ func createDecoderOfType(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
}
}
func encoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
func encoderOfType(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
typeName := typ.String()
typeEncoder := typeEncoders[typeName]
if typeEncoder != nil {
@ -418,7 +418,7 @@ func encoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
return newEncoder, err
}
func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
if typ.String() == "[]uint8" {
return &base64Codec{}, nil
}
@ -433,7 +433,7 @@ func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
}
if typ.ConvertibleTo(marshalerType) {
templateInterface := reflect.New(typ).Elem().Interface()
var encoder Encoder = &marshalerEncoder{extractInterface(templateInterface)}
var encoder ValEncoder = &marshalerEncoder{extractInterface(templateInterface)}
if typ.Kind() != reflect.Struct {
encoder = &optionalEncoder{encoder}
}
@ -493,7 +493,7 @@ func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
}
}
func decoderOfOptional(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
func decoderOfOptional(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
elemType := typ.Elem()
decoder, err := decoderOfType(cfg, elemType)
if err != nil {
@ -502,7 +502,7 @@ func decoderOfOptional(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
return &optionalDecoder{elemType, decoder}, nil
}
func encoderOfOptional(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
func encoderOfOptional(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
elemType := typ.Elem()
elemEncoder, err := encoderOfType(cfg, elemType)
if err != nil {
@ -515,7 +515,7 @@ func encoderOfOptional(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
return encoder, nil
}
func decoderOfMap(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
func decoderOfMap(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
decoder, err := decoderOfType(cfg, typ.Elem())
if err != nil {
return nil, err
@ -528,7 +528,7 @@ func extractInterface(val interface{}) emptyInterface {
return *((*emptyInterface)(unsafe.Pointer(&val)))
}
func encoderOfMap(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
func encoderOfMap(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
elemType := typ.Elem()
encoder, err := encoderOfType(cfg, elemType)
if err != nil {

View File

@ -7,7 +7,7 @@ import (
"unsafe"
)
func decoderOfArray(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
func decoderOfArray(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
decoder, err := decoderOfType(cfg, typ.Elem())
if err != nil {
return nil, err
@ -15,7 +15,7 @@ func decoderOfArray(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
return &arrayDecoder{typ, typ.Elem(), decoder}, nil
}
func encoderOfArray(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
func encoderOfArray(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
encoder, err := encoderOfType(cfg, typ.Elem())
if err != nil {
return nil, err
@ -29,7 +29,7 @@ func encoderOfArray(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
type arrayEncoder struct {
arrayType reflect.Type
elemType reflect.Type
elemEncoder Encoder
elemEncoder ValEncoder
}
func (encoder *arrayEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
@ -62,7 +62,7 @@ func (encoder *arrayEncoder) isEmpty(ptr unsafe.Pointer) bool {
type arrayDecoder struct {
arrayType reflect.Type
elemType reflect.Type
elemDecoder Decoder
elemDecoder ValDecoder
}
func (decoder *arrayDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {

View File

@ -13,7 +13,7 @@ type mapDecoder struct {
mapType reflect.Type
keyType reflect.Type
elemType reflect.Type
elemDecoder Decoder
elemDecoder ValDecoder
mapInterface emptyInterface
}
@ -72,7 +72,7 @@ func (decoder *mapDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
type mapEncoder struct {
mapType reflect.Type
elemType reflect.Type
elemEncoder Encoder
elemEncoder ValEncoder
mapInterface emptyInterface
}
@ -141,7 +141,7 @@ func (encoder *mapEncoder) isEmpty(ptr unsafe.Pointer) bool {
type sortKeysMapEncoder struct {
mapType reflect.Type
elemType reflect.Type
elemEncoder Encoder
elemEncoder ValEncoder
mapInterface emptyInterface
}

View File

@ -447,7 +447,7 @@ func (encoder *base64Codec) isEmpty(ptr unsafe.Pointer) bool {
}
type stringModeDecoder struct {
elemDecoder Decoder
elemDecoder ValDecoder
}
func (decoder *stringModeDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
@ -468,7 +468,7 @@ func (decoder *stringModeDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
}
type stringModeEncoder struct {
elemEncoder Encoder
elemEncoder ValEncoder
}
func (encoder *stringModeEncoder) encode(ptr unsafe.Pointer, stream *Stream) {

View File

@ -9,7 +9,7 @@ import (
"unsafe"
)
func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
structEncoder_ := &structEncoder{}
fields := map[string]*structFieldEncoder{}
for _, field := range listStructFields(typ) {
@ -86,7 +86,7 @@ func listStructFields(typ reflect.Type) []*reflect.StructField {
return fields
}
func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
func decoderOfStruct(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
fields := map[string]*structFieldDecoder{}
for _, field := range listStructFields(typ) {
fieldDecoderKey := fmt.Sprintf("%s/%s", typ.String(), field.Name)
@ -155,7 +155,7 @@ func calcFieldNames(originalFieldName string, tagProvidedFieldName string, exten
return fieldNames
}
func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (Decoder, error) {
func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder) (ValDecoder, error) {
knownHash := map[int32]struct{}{
0: {},
}
@ -1059,7 +1059,7 @@ func (decoder *tenFieldsStructDecoder) decode(ptr unsafe.Pointer, iter *Iterator
type structFieldDecoder struct {
field *reflect.StructField
fieldDecoder Decoder
fieldDecoder ValDecoder
}
func (decoder *structFieldDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
@ -1073,7 +1073,7 @@ func (decoder *structFieldDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
type structFieldEncoder struct {
field *reflect.StructField
fieldName string
fieldEncoder Encoder
fieldEncoder ValEncoder
omitempty bool
}
@ -1116,7 +1116,7 @@ func (encoder *structEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
}
func (encoder *structEncoder) encodeInterface(val interface{}, stream *Stream) {
var encoderToUse Encoder
var encoderToUse ValEncoder
encoderToUse = encoder
if len(encoder.fields) == 1 {
firstEncoder := encoder.fields[0].fieldEncoder

View File

@ -7,7 +7,7 @@ import (
"unsafe"
)
func decoderOfSlice(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
func decoderOfSlice(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
decoder, err := decoderOfType(cfg, typ.Elem())
if err != nil {
return nil, err
@ -15,7 +15,7 @@ func decoderOfSlice(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
return &sliceDecoder{typ, typ.Elem(), decoder}, nil
}
func encoderOfSlice(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
func encoderOfSlice(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
encoder, err := encoderOfType(cfg, typ.Elem())
if err != nil {
return nil, err
@ -29,7 +29,7 @@ func encoderOfSlice(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
type sliceEncoder struct {
sliceType reflect.Type
elemType reflect.Type
elemEncoder Encoder
elemEncoder ValEncoder
}
func (encoder *sliceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
@ -68,7 +68,7 @@ func (encoder *sliceEncoder) isEmpty(ptr unsafe.Pointer) bool {
type sliceDecoder struct {
sliceType reflect.Type
elemType reflect.Type
elemDecoder Decoder
elemDecoder ValDecoder
}
// sliceHeader is a safe version of SliceHeader used within this package.