-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
59 lines (50 loc) · 1.13 KB
/
options.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
49
50
51
52
53
54
55
56
57
58
59
package jsonKit
import jsoniter "github.com/json-iterator/go"
type (
jsonOptions struct {
api jsoniter.API
// 目前只能是 ""
prefix string
/*
(1) 目前只能是 "" 或 多个空格(不能有其他字符)
(2) encoding/json标准库 可以用"\t"
*/
indent string
}
JsonOption func(opts *jsonOptions)
)
func loadOptions(options ...JsonOption) *jsonOptions {
opts := &jsonOptions{
api: nil,
prefix: "",
indent: "",
}
for _, option := range options {
option(opts)
}
// 容错,以防调用方瞎搞
if opts.api == nil {
opts.api = jsoniter.ConfigDefault
}
return opts
}
// WithApi
/*
@param api jsoniter.ConfigDefault || jsoniter.ConfigCompatibleWithStandardLibrary || jsoniter.ConfigFastest
*/
func WithApi(api jsoniter.API) JsonOption {
return func(opts *jsonOptions) {
opts.api = api
}
}
// WithIndent
/*
@param indent (0) 建议使用 " "(4个空格)
(1) 目前只能是 "" 或 多个空格(不能有其他字符)
(2) encoding/json标准库 可以用"\t"
*/
func WithIndent(indent string) JsonOption {
return func(opts *jsonOptions) {
opts.indent = indent
}
}