You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-06-15 22:50:24 +02:00
Fix standard compatiblility
Non-nil but empty map with omitempty should be ignored. Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
This commit is contained in:
@ -18,3 +18,23 @@ func TestEncoderHasTrailingNewline(t *testing.T) {
|
||||
stdenc.Encode(1)
|
||||
should.Equal(stdbuf.Bytes(), buf.Bytes())
|
||||
}
|
||||
|
||||
// Non-nil but empty map should be ignored.
|
||||
func TestOmitempty(t *testing.T) {
|
||||
o := struct {
|
||||
A string `json:"a,omitempty"`
|
||||
B string `json:"b,omitempty"`
|
||||
Annotations map[string]string `json:"annotations,omitempty"`
|
||||
}{
|
||||
A: "a",
|
||||
B: "b",
|
||||
Annotations: map[string]string{},
|
||||
}
|
||||
should := require.New(t)
|
||||
var buf, stdbuf bytes.Buffer
|
||||
enc := ConfigCompatibleWithStandardLibrary.NewEncoder(&buf)
|
||||
enc.Encode(o)
|
||||
stdenc := json.NewEncoder(&stdbuf)
|
||||
stdenc.Encode(o)
|
||||
should.Equal(string(stdbuf.Bytes()), string(buf.Bytes()))
|
||||
}
|
||||
|
Reference in New Issue
Block a user