/
json.go
158 lines (128 loc) · 2.79 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package json
import (
"bytes"
"fmt"
"io"
"os"
"strings"
gojson "github.com/goccy/go-json"
"github.com/spf13/afero"
"github.com/tidwall/gjson"
)
var (
fs = afero.NewOsFs()
)
type Raw = gojson.RawMessage
type Result struct {
gjson.Result
}
type MarshalJSON = gojson.Marshaler
type UnmarshalJSON = gojson.Unmarshaler
func Marshal(v interface{}) ([]byte, error) {
return gojson.Marshal(v)
}
func Unmarshal(data []byte, v interface{}) error {
return gojson.Unmarshal(data, v)
}
func Compact(dst *bytes.Buffer, src RawValue) error {
return gojson.Compact(dst, src)
}
func EncodeWithColor(v any, pretty ...bool) (r RawValue) {
if len(pretty) > 0 && pretty[0] {
r, _ = gojson.MarshalIndentWithOption(v, "", " ", gojson.Colorize(gojson.DefaultColorScheme))
return
}
r, _ = gojson.MarshalWithOption(v, gojson.Colorize(gojson.DefaultColorScheme))
return
}
func EncodeNoEscape(v any) (r RawValue) {
r, _ = gojson.MarshalNoEscape(v)
return
}
func MapToStruct(m any, target any) (err error) {
err = Decode(Encode(m), target)
return
}
func Encode(v interface{}, pretty ...bool) (r RawValue) {
if len(pretty) > 0 {
if r, e := gojson.MarshalIndent(v, "", " "); e != nil {
//fmt.Println(e.Error())
return nil
} else {
return r
}
}
r, _ = gojson.Marshal(v)
return
}
func EncodePrefix(v interface{}, prefix string, pretty ...bool) (r []byte) {
if len(pretty) > 0 {
r, _ = gojson.MarshalIndent(v, prefix, " ")
return
}
r, _ = gojson.Marshal(v)
return
}
func EncodeAndStore(v interface{}, w interface{}, pretty ...bool) (r []byte, err error) {
if len(pretty) > 0 {
r, _ = gojson.MarshalIndent(v, "", " ")
return
}
r, _ = gojson.Marshal(v)
switch va := w.(type) {
case io.Writer:
_, err = va.Write(r)
default:
err = Save(va.(string), r)
}
return
}
func Decode(data []byte, v interface{}) (err error) {
err = gojson.Unmarshal(data, v)
return
}
func Load(src string) (data RawValue) {
if strings.HasPrefix(src, "file:") {
src = strings.TrimPrefix(src, "file:")
}
if d, e := afero.ReadFile(fs, src); e != nil {
return
} else {
data = d
}
return
}
func LoadWithErr(src string) (data RawValue, err error) {
if strings.HasPrefix(src, "file:") {
src = strings.TrimPrefix(src, "file:")
}
if d, e := afero.ReadFile(fs, src); e != nil {
err = e
return
} else {
data = d
}
return
}
func Save(src string, data []byte) (err error) {
fmt.Println("saving json", src)
err = afero.WriteFile(fs, src, data, os.FileMode(0755))
return
}
func Valid(src []byte) bool {
return gojson.Valid(src)
}
func Read(src string, v interface{}) (err error) {
if d, e := afero.ReadFile(fs, src); e != nil {
err = e
return
} else {
Decode(d, v)
}
return
}
func GetFromFile(src string, path string) (g gjson.Result) {
buf := Load(src)
g = gjson.GetBytes(buf, path)
return
}