Ver Fonte

add isEmptyFunc

Tao Wen há 8 anos atrás
pai
commit
43a832beee
3 ficheiros alterados com 14 adições e 10 exclusões
  1. 9 5
      feature_reflect.go
  2. 2 2
      feature_reflect_object.go
  3. 3 3
      jsoniter_customize_test.go

+ 9 - 5
feature_reflect.go

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

+ 2 - 2
feature_reflect_object.go

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

+ 3 - 3
jsoniter_customize_test.go

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