diff --git a/njson/json.go b/njson/json.go index 67d93d8..0c55fa2 100644 --- a/njson/json.go +++ b/njson/json.go @@ -23,10 +23,23 @@ func MarshalStr(v any) (string, error) { return string(data), nil } +// MarshalSafe marshals v into json bytes. +// return an empty byte slice if an error occurred. +func MarshalSafe(v any) []byte { + ret, err := Marshal(v) + if err != nil { + return []byte{} + } + return ret +} + // MarshalStrSafe marshals v into a string. // return an empty string if an error occurred. func MarshalStrSafe(v any) string { - ret, _ := MarshalStr(v) + ret, err := MarshalStr(v) + if err != nil { + return "" + } return ret }