-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson.go
More file actions
152 lines (140 loc) · 3.16 KB
/
json.go
File metadata and controls
152 lines (140 loc) · 3.16 KB
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
package com
import (
"fmt"
"io"
"os"
"strings"
"github.com/webx-top/com/encoding/json"
)
// GetJSON Read json data, writes in struct f
func GetJSON(dat *string, s interface{}) error {
return json.Unmarshal([]byte(*dat), s)
}
// UnmarshalStream .
func UnmarshalStream(r io.Reader, m interface{}, fn func()) error {
dec := json.NewDecoder(r)
for {
if err := dec.Decode(m); err != nil {
if err == io.EOF {
break
}
return err
}
fn()
}
return nil
}
// MarshalStream .
func MarshalStream(w io.Writer, m interface{}) error {
enc := json.NewEncoder(w)
return enc.Encode(m)
}
// SetJSON Struct s will be converted to json format
func SetJSON(s interface{}) (string, error) {
dat, err := json.Marshal(s)
return string(dat), err
}
// ReadJSON Json data read from the specified file
func ReadJSON(path string, s interface{}) error {
dat, err1 := os.ReadFile(path)
if err1 != nil {
return err1
}
return json.Unmarshal(dat, s)
}
// WriteJSON The json data is written to the specified file
func WriteJSON(path string, dat *string) error {
_, err0 := os.Stat(path)
if err0 != nil || !os.IsExist(err0) {
os.Create(path)
}
return os.WriteFile(path, []byte(*dat), 0644)
}
// Dump 输出对象和数组的结构信息
func Dump(m interface{}, args ...bool) (r string) {
v, err := json.MarshalIndent(m, "", " ")
if err != nil {
fmt.Printf("%v\n", err)
}
r = string(v)
l := len(args)
if l < 1 || args[0] {
fmt.Println(r)
}
return
}
var jsonArrayReplacer = strings.NewReplacer(
`[`, ``,
`]`, ``,
`"`, ``,
`'`, ``,
"`", ``,
`\`, ``,
)
func ListToJSONArray(list string, unique ...bool) string {
items := strings.Split(list, `,`)
result := make([]string, 0, len(items))
if len(unique) > 0 && unique[0] {
uniqMap := map[string]struct{}{}
for _, item := range items {
item = strings.TrimSpace(item)
item = jsonArrayReplacer.Replace(item)
if len(item) == 0 {
continue
}
if _, ok := uniqMap[item]; ok {
continue
}
result = append(result, item)
uniqMap[item] = struct{}{}
}
list, _ = SetJSON(result)
return list
}
for _, item := range items {
item = strings.TrimSpace(item)
item = jsonArrayReplacer.Replace(item)
if len(item) == 0 {
continue
}
result = append(result, item)
}
list, _ = SetJSON(result)
return list
}
func ListToJSONUintArray(list string, unique ...bool) string {
items := strings.Split(list, `,`)
result := make([]uint64, 0, len(items))
if len(unique) > 0 && unique[0] {
uniqMap := map[uint64]struct{}{}
for _, item := range items {
item = strings.TrimSpace(item)
if len(item) == 0 {
continue
}
i := Uint64(item)
if i == 0 {
continue
}
if _, ok := uniqMap[i]; ok {
continue
}
result = append(result, i)
uniqMap[i] = struct{}{}
}
list, _ = SetJSON(result)
return list
}
for _, item := range items {
item = strings.TrimSpace(item)
if len(item) == 0 {
continue
}
i := Uint64(item)
if i == 0 {
continue
}
result = append(result, i)
}
return `[` + JoinNumbers(result, `,`) + `]`
}