1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-23 11:37:32 +02:00

add isEmptyFunc

This commit is contained in:
Tao Wen 2017-06-20 07:57:23 +08:00
parent 39c9bb226a
commit 43a832beee
3 changed files with 14 additions and 10 deletions

View File

@ -59,6 +59,7 @@ func (decoder *funcDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
type funcEncoder struct { type funcEncoder struct {
fun EncoderFunc fun EncoderFunc
isEmptyFunc func(ptr unsafe.Pointer) bool
} }
func (encoder *funcEncoder) encode(ptr unsafe.Pointer, stream *Stream) { func (encoder *funcEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
@ -70,8 +71,11 @@ func (encoder *funcEncoder) encodeInterface(val interface{}, stream *Stream) {
} }
func (encoder *funcEncoder) isEmpty(ptr unsafe.Pointer) bool { func (encoder *funcEncoder) isEmpty(ptr unsafe.Pointer) bool {
if encoder.isEmptyFunc == nil {
return false return false
} }
return encoder.isEmptyFunc(ptr)
}
var typeDecoders map[string]Decoder var typeDecoders map[string]Decoder
var fieldDecoders map[string]Decoder var fieldDecoders map[string]Decoder
@ -111,12 +115,12 @@ func RegisterFieldDecoder(typ string, field string, fun DecoderFunc) {
fieldDecoders[fmt.Sprintf("%s/%s", typ, field)] = &funcDecoder{fun} fieldDecoders[fmt.Sprintf("%s/%s", typ, field)] = &funcDecoder{fun}
} }
func RegisterTypeEncoder(typ string, fun EncoderFunc) { func RegisterTypeEncoder(typ string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
typeEncoders[typ] = &funcEncoder{fun} typeEncoders[typ] = &funcEncoder{fun, isEmptyFunc}
} }
func RegisterFieldEncoder(typ string, field string, fun EncoderFunc) { func RegisterFieldEncoder(typ string, field string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
fieldEncoders[fmt.Sprintf("%s/%s", typ, field)] = &funcEncoder{fun} fieldEncoders[fmt.Sprintf("%s/%s", typ, field)] = &funcEncoder{fun, isEmptyFunc}
} }
// RegisterExtension can register a custom extension // RegisterExtension can register a custom extension

View File

@ -21,7 +21,7 @@ func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
extensionProvidedFieldNames = alternativeFieldNames extensionProvidedFieldNames = alternativeFieldNames
} }
if fun != nil { if fun != nil {
fieldEncoders[fieldEncoderKey] = &funcEncoder{fun} fieldEncoders[fieldEncoderKey] = &funcEncoder{fun, nil}
} }
} }
for _, extension := range cfg.extensions { for _, extension := range cfg.extensions {
@ -30,7 +30,7 @@ func encoderOfStruct(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
extensionProvidedFieldNames = alternativeFieldNames extensionProvidedFieldNames = alternativeFieldNames
} }
if fun != nil { if fun != nil {
fieldEncoders[fieldEncoderKey] = &funcEncoder{fun} fieldEncoders[fieldEncoderKey] = &funcEncoder{fun, nil}
} }
} }
tagParts := strings.Split(field.Tag.Get("json"), ",") tagParts := strings.Split(field.Tag.Get("json"), ",")

View File

@ -36,7 +36,7 @@ func Test_customize_type_encoder(t *testing.T) {
RegisterTypeEncoder("time.Time", func(ptr unsafe.Pointer, stream *Stream) { RegisterTypeEncoder("time.Time", func(ptr unsafe.Pointer, stream *Stream) {
t := *((*time.Time)(ptr)) t := *((*time.Time)(ptr))
stream.WriteString(t.UTC().Format("2006-01-02 15:04:05")) stream.WriteString(t.UTC().Format("2006-01-02 15:04:05"))
}) }, nil)
defer ConfigDefault.cleanEncoders() defer ConfigDefault.cleanEncoders()
val := time.Unix(0, 0) val := time.Unix(0, 0)
str, err := MarshalToString(val) str, err := MarshalToString(val)
@ -50,7 +50,7 @@ func Test_customize_byte_array_encoder(t *testing.T) {
RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) { RegisterTypeEncoder("[]uint8", func(ptr unsafe.Pointer, stream *Stream) {
t := *((*[]byte)(ptr)) t := *((*[]byte)(ptr))
stream.WriteString(string(t)) stream.WriteString(string(t))
}) }, nil)
defer ConfigDefault.cleanEncoders() defer ConfigDefault.cleanEncoders()
val := []byte("abc") val := []byte("abc")
str, err := MarshalToString(val) str, err := MarshalToString(val)
@ -158,7 +158,7 @@ func Test_marshaler_and_encoder(t *testing.T) {
should := require.New(t) should := require.New(t)
RegisterTypeEncoder("jsoniter.ObjectImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) { RegisterTypeEncoder("jsoniter.ObjectImplementedMarshaler", func(ptr unsafe.Pointer, stream *Stream) {
stream.WriteString("hello from encoder") stream.WriteString("hello from encoder")
}) }, nil)
val := ObjectImplementedMarshaler(100) val := ObjectImplementedMarshaler(100)
obj := TestObject{&val} obj := TestObject{&val}
bytes, err := json.Marshal(obj) bytes, err := json.Marshal(obj)