mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
commit
abcf2759ed
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user