-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer_json.go
68 lines (58 loc) · 1.51 KB
/
serializer_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cdt
import (
"encoding/json"
"fmt"
)
// String return string value
func (i convert) String() string {
return fmt.Sprintf("%v", i.OriginVal)
}
// MarshalJSON to output non base64 encoded []byte
func (c DataRaw) MarshalJSON() ([]byte, error) {
return json.Marshal(c.OriginVal)
}
// UnmarshalJSON to deserialize []byte
func (c *DataRaw) UnmarshalJSON(b []byte) error {
err := json.Unmarshal(b, &c.OriginVal)
if err != nil {
return err
}
return nil
}
// String return string value
func (c DataRaw) String() string {
return fmt.Sprintf("%v", c.OriginVal)
}
// MarshalJSON marshal json
func MarshalJSON(data any) ([]byte, error) {
newData := CopyVariable(data)
EncodeDataForTags(newData)
return json.Marshal(newData)
}
// MarshalJSONToString marshal json to string
func MarshalJSONToString(data any) (string, error) {
result, err := MarshalJSON(data)
if err != nil {
return "", err
}
return string(result), nil
}
// MarshalJSONToIndent marshal json to string
func MarshalJSONToIndent(data any, prefix, indent string) ([]byte, error) {
newData := CopyVariable(data)
EncodeDataForTags(newData)
return json.MarshalIndent(newData, prefix, indent)
}
// UnmarshalJSONFromString unmarshal json from string
func UnmarshalJSONFromString(data string, v any) error {
return UnmarshalJSONFromByte([]byte(data), v)
}
// UnmarshalJSONFromByte unmarshal json from byte
func UnmarshalJSONFromByte(data []byte, v any) error {
err := json.Unmarshal(data, v)
if err != nil {
return err
}
DecodeDataForTags(v)
return nil
}