Tao Wen 8 năm trước cách đây
mục cha
commit
ccb972f58c
2 tập tin đã thay đổi với 6 bổ sung5 xóa
  1. 4 4
      feature_reflect_object.go
  2. 2 1
      jsoniter_reflect_struct_test.go

+ 4 - 4
feature_reflect_object.go

@@ -1058,12 +1058,12 @@ func (encoder *structEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
 	stream.WriteObjectStart()
 	isNotFirst := false
 	for _, field := range encoder.fields {
-		if isNotFirst {
-			stream.WriteMore()
-		}
 		if field.omitempty && field.isEmpty(ptr) {
 			continue
 		}
+		if isNotFirst {
+			stream.WriteMore()
+		}
 		field.encode(ptr, stream)
 		isNotFirst = true
 	}
@@ -1097,4 +1097,4 @@ func (encoder *emptyStructEncoder) encodeInterface(val interface{}, stream *Stre
 
 func (encoder *emptyStructEncoder) isEmpty(ptr unsafe.Pointer) bool {
 	return true
-}
+}

+ 2 - 1
jsoniter_reflect_struct_test.go

@@ -165,6 +165,7 @@ func Test_omit_empty(t *testing.T) {
 	type TestObject struct {
 		Field1 string `json:"field-1,omitempty"`
 		Field2 string `json:"field-2,omitempty"`
+		Field3 string `json:"field-3,omitempty"`
 	}
 	obj := TestObject{}
 	obj.Field2 = "hello"
@@ -184,4 +185,4 @@ func Test_any_within_struct(t *testing.T) {
 	should.Nil(err)
 	should.Equal("hello", obj.Field1.ToString())
 	should.Equal("[1,2,3]", obj.Field2.ToString())
-}
+}