go icon indicating copy to clipboard operation
go copied to clipboard

serialize map omitempty

Open philhuan opened this issue 3 years ago • 0 comments

When serializing a map, Is there a way to ignore zero values ​​in map? I don't want zero values ​​inside map

example:

func TestMarshal(t *testing.T) {
	m := map[string]interface{}{
		"foo": "bar",
		"int": 0,
		"n":   nil,
	}

	bytes, err := jsoniter.Marshal(m)
	assert.NoError(t, err)
	t.Log(string(bytes))
}

output: {"int":0,"n":null,"foo":"bar"}

want: {"foo":"bar"}

philhuan avatar Jul 27 '22 10:07 philhuan