-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
54 lines (45 loc) · 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
49
50
51
52
53
54
package web
import (
"bytes"
"encoding/json"
)
// json.Marshal和json.Unmarshal处理int64有bug
func JsonEncode(v interface{}) ([]byte, error) {
b := new(bytes.Buffer)
e := json.NewEncoder(b)
// https://studygolang.com/articles/14473?fr=sidebar
e.SetEscapeHTML(false) // fix: encoding 时可能会自动追加 '\n'
err := e.Encode(v)
if err != nil {
return nil, err
}
blob := b.Bytes()
if len(blob) > 0 && blob[len(blob)-1] == '\n' {
blob = blob[:len(blob)-1]
}
return blob, nil
}
func MustJsonEncode(v interface{}) []byte {
b, e := JsonEncode(v)
Raise(e)
return b
}
func JsonDecode(data []byte, v interface{}) error {
e := json.NewDecoder(bytes.NewBuffer(data))
e.UseNumber()
if err := e.Decode(v); err != nil {
return err
}
return nil
}
func MustJsonDecode(data []byte, v interface{}) {
e := json.NewDecoder(bytes.NewBuffer(data))
e.UseNumber()
if err := e.Decode(v); err != nil {
Raise(err)
}
}
func JsonString(v interface{}) string {
b, _ := json.Marshal(v)
return string(b)
}