-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
executable file
·74 lines (64 loc) · 1.71 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package gjson
import (
"encoding/json"
"fmt"
"github.com/davidforest123/goutil/basic/gerrors"
"github.com/mailru/easyjson"
"reflect"
"strings"
)
func MarshalBytes(v interface{}, indent bool) ([]byte, error) {
var buf []byte
err := error(nil)
if indent {
buf, err = json.MarshalIndent(v, "", "\t")
} else {
buf, err = json.Marshal(v)
}
if err != nil {
return nil, err
}
return buf, nil
}
func MarshalString(v interface{}, indent bool) (string, error) {
b, err := MarshalBytes(v, indent)
if err != nil {
return "", err
}
return string(b), nil
}
func MarshalBytesWithErrFmt(v interface{}, indent bool, errFmt string) []byte {
var buf []byte
err := error(nil)
if indent {
buf, err = json.MarshalIndent(v, "", "\t")
} else {
buf, err = json.Marshal(v)
}
if err != nil {
return []byte(fmt.Sprintf(errFmt, err.Error()))
}
return buf
}
func MarshalStringWithErrFmt(v interface{}, indent bool, errFmt string) string {
return string(MarshalBytesWithErrFmt(v, indent, errFmt))
}
func MarshalBytesDefault(v interface{}, indent bool) []byte {
return MarshalBytesWithErrFmt(v, indent, `{"Error":"%s"}`)
}
func MarshalStringDefault(v interface{}, indent bool) string {
return MarshalStringWithErrFmt(v, indent, `{"Error":"%s"}`)
}
func MarshalBytesFast(v easyjson.Marshaler) ([]byte, error) {
return easyjson.Marshal(v)
}
func UnmarshalBytesFast(data []byte, v easyjson.Unmarshaler) error {
return easyjson.Unmarshal(data, v)
}
// JSONDecode decodes JSON data into a structure
func JSONDecode(data []byte, to interface{}) error {
if !strings.Contains(reflect.ValueOf(to).Type().String(), "*") {
return gerrors.New("json decode error - memory address not supplied")
}
return json.Unmarshal(data, to)
}