From b11711335c18931ad9ec5bdeb7bd8c5f261407a8 Mon Sep 17 00:00:00 2001 From: "torben.diekmann" Date: Tue, 9 May 2023 11:05:57 +0200 Subject: [PATCH 1/2] marshals nil slices/arrays like expected and equivalent to json.marshal --- sheriff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sheriff.go b/sheriff.go index 8ce11db..022a8d7 100644 --- a/sheriff.go +++ b/sheriff.go @@ -218,7 +218,7 @@ func marshalValue(options *Options, v reflect.Value) (interface{}, error) { k := v.Kind() switch k { - case reflect.Interface, reflect.Map, reflect.Ptr: + case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: if v.IsNil() { return val, nil } From f4025539959e102d90a06992f6dadcee61477e42 Mon Sep 17 00:00:00 2001 From: "torben.diekmann" Date: Tue, 9 May 2023 11:05:57 +0200 Subject: [PATCH 2/2] marshals nil slices/arrays like expected and equivalent to json.marshal --- sheriff_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sheriff_test.go b/sheriff_test.go index d079e4b..f54a3ef 100644 --- a/sheriff_test.go +++ b/sheriff_test.go @@ -743,6 +743,20 @@ func TestMarshal_NilSlice(t *testing.T) { jsonResult, err := json.Marshal(marshalSlice) assert.NoError(t, err) + expect := "null" + + assert.Equal(t, expect, string(jsonResult)) +} + +func TestMarshal_EmptySlice(t *testing.T) { + var stringSlice = []string{} // empty slice + + marshalSlice, err := Marshal(&Options{}, stringSlice) + assert.NoError(t, err) + + jsonResult, err := json.Marshal(marshalSlice) + assert.NoError(t, err) + expect := "[]" assert.Equal(t, expect, string(jsonResult))