1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00

Merge pull request #14 from eruca/master

fix the omitempty bug
This commit is contained in:
Tao Wen 2017-04-18 10:23:05 +10:00 committed by GitHub
commit abcf2759ed
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,10 +165,11 @@ 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"
str, err := MarshalToString(&obj)
should.Nil(err)
should.Equal(`{"field-2":"hello"}`, str)
}
}