-
Notifications
You must be signed in to change notification settings - Fork 3
/
marshal.go
48 lines (41 loc) · 1.13 KB
/
marshal.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 sonicKit
import "github.com/bytedance/sonic"
var (
Marshal func(v interface{}) ([]byte, error) = sonic.Marshal
MarshalToString func(v interface{}) (string, error) = sonic.MarshalString
)
func MarshalByAPI(api sonic.API, v interface{}) ([]byte, error) {
if api == nil {
api = sonic.ConfigDefault
}
return api.Marshal(v)
}
func MarshalToStringByAPI(api sonic.API, v interface{}) (string, error) {
if api == nil {
api = sonic.ConfigDefault
}
return api.MarshalToString(v)
}
// MarshalByAPIWithIndent
/*
@param prefix 一般为""
@param indent 推荐值: "\t" || " "(4个空格)
*/
func MarshalByAPIWithIndent(api sonic.API, v interface{}, prefix, indent string) ([]byte, error) {
if api == nil {
api = sonic.ConfigDefault
}
return api.MarshalIndent(v, prefix, indent)
}
// MarshalToStringByAPIWithIndent
/*
@param prefix 一般为""
@param indent 推荐值: "\t" || " "(4个空格)
*/
func MarshalToStringByAPIWithIndent(api sonic.API, v interface{}, prefix, indent string) (string, error) {
data, err := MarshalByAPIWithIndent(api, v, prefix, indent)
if err != nil {
return "", err
}
return string(data), nil
}