feat: 添加 njson.MarshalSafe 方法,同时修复之前的MarshalStrSafe方法

main v1.1.6
NoahLan 1 year ago
parent b7c759db67
commit 2a31984fc7

@ -23,10 +23,23 @@ func MarshalStr(v any) (string, error) {
return string(data), nil 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. // MarshalStrSafe marshals v into a string.
// return an empty string if an error occurred. // return an empty string if an error occurred.
func MarshalStrSafe(v any) string { func MarshalStrSafe(v any) string {
ret, _ := MarshalStr(v) ret, err := MarshalStr(v)
if err != nil {
return ""
}
return ret return ret
} }

Loading…
Cancel
Save