-
Notifications
You must be signed in to change notification settings - Fork 3
/
gojson.go
55 lines (45 loc) · 1.12 KB
/
gojson.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
package gojson
import "github.com/dtapps/go-library/utils/gojson/json"
func Marshal(v any) ([]byte, error) {
return json.Marshal(v)
}
func Unmarshal(data []byte, v any) error {
return json.Unmarshal(data, v)
}
func Encode(v interface{}) (string, error) {
bytes, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(bytes), nil
}
func MarshalToString(msg interface{}) (string, error) {
j, e := json.Marshal(msg)
if e != nil {
return "", e
}
return string(j), nil
}
func JsonDecode(data string) (map[string]interface{}, error) {
var dat map[string]interface{}
err := json.Unmarshal([]byte(data), &dat)
return dat, err
}
func JsonDecodeNoError(data string) map[string]interface{} {
var dat map[string]interface{}
_ = json.Unmarshal([]byte(data), &dat)
return dat
}
func JsonEncode(data interface{}) (string, error) {
jsons, err := json.Marshal(data)
return string(jsons), err
}
func JsonEncodeNoError(data interface{}) string {
jsons, _ := json.Marshal(data)
return string(jsons)
}
func JsonDecodesNoError(data string) []string {
var dat []string
_ = json.Unmarshal([]byte(data), &dat)
return dat
}