-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
69 lines (55 loc) · 1.44 KB
/
api.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
60
61
62
63
64
65
66
67
68
69
package jsonKit
import (
"github.com/richelieu-yang/chimera/v2/src/compareKit"
"github.com/richelieu-yang/chimera/v2/src/consts"
"github.com/sirupsen/logrus"
)
var library string
// defaultApi 默认的API
var defaultApi API = nil
// stdApi 标准的API(会对map的keys排序)
var stdApi API = nil
type (
API interface {
Marshal(v interface{}) ([]byte, error)
MarshalIndent(v interface{}, prefix, indent string) ([]byte, error)
MarshalToString(v interface{}) (string, error)
Unmarshal(data []byte, v interface{}) error
UnmarshalFromString(str string, v interface{}) error
}
)
func GetLibrary() string {
return library
}
func GetDefaultApi() API {
return defaultApi
}
func GetStdApi() API {
return stdApi
}
func testAPI() {
api := defaultApi
m := map[string]interface{}{
"0": 3.1415926,
"1": 1,
"2": true,
"3": `~!@#$%^&*()_+{}|:"><?
qwdqw强 \t\r\n无敌
qwdqwd
威尔法
496465~·《》?L:"{}|,./l;'[]\/*-的确
`,
}
jsonData, err := api.Marshal(m)
if err != nil {
logrus.WithError(err).Fatalf("[%s, JSON] Fail to marshal!!!", consts.UpperProjectName)
}
var m1 map[string]interface{}
if err := api.Unmarshal(jsonData, &m1); err != nil {
logrus.WithError(err).Fatalf("[%s, JSON] Fail to unmarshal!!!", consts.UpperProjectName)
}
if compareKit.EqualWithTypeAndValue(m, m1) {
diff := compareKit.Diff(m, m1)
logrus.WithField("diff", diff).Fatalf("[%s, JSON] m and m1 are different.", consts.UpperProjectName)
}
}