1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-20 18:48:32 +02:00
This commit is contained in:
Tao Wen 2017-04-28 09:10:06 +08:00
commit ccb972f58c
2 changed files with 6 additions and 5 deletions

View File

@ -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
}
}

View File

@ -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())
}
}