diff --git a/utils/super/json.go b/utils/super/json.go new file mode 100644 index 0000000..446f7ea --- /dev/null +++ b/utils/super/json.go @@ -0,0 +1,41 @@ +package super + +import ( + jsonIter "github.com/json-iterator/go" + "reflect" +) + +var json = jsonIter.ConfigCompatibleWithStandardLibrary + +// MarshalJSON 将对象转换为 json +func MarshalJSON(v interface{}) []byte { + b, err := json.Marshal(v) + if err != nil { + switch reflect.TypeOf(v).Kind() { + case reflect.Array, reflect.Slice: + return StringToBytes("[]") + default: + return StringToBytes("{}") + } + } + return b +} + +// UnmarshalJSON 将 json 转换为对象 +func UnmarshalJSON(data []byte, v interface{}) error { + return json.Unmarshal(data, v) +} + +// MarshalIndentJSON 将对象转换为 json +func MarshalIndentJSON(v interface{}, prefix, indent string) []byte { + b, err := json.MarshalIndent(v, prefix, indent) + if err != nil { + switch reflect.TypeOf(v).Kind() { + case reflect.Array, reflect.Slice: + return StringToBytes("[]") + default: + return StringToBytes("{}") + } + } + return b +}