Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

marshals nil slices/arrays like expected and equivalent to json.marshal #40

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sheriff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions sheriff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down