-
Notifications
You must be signed in to change notification settings - Fork 16
/
json.go
48 lines (42 loc) · 1.1 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package super
import (
jsonIter "github.com/json-iterator/go"
"reflect"
)
var json = jsonIter.ConfigCompatibleWithStandardLibrary
// MarshalJSON 将对象转换为 json
// - 当转换失败时,将返回 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
}
// MarshalJSONE 将对象转换为 json
// - 当转换失败时,将返回错误信息
func MarshalJSONE(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// 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
}