/
json_class.go
218 lines (180 loc) · 4.74 KB
/
json_class.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package json
import (
"bytes"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/mattn/go-colorable"
"github.com/neilotoole/jsoncolor"
)
type Json string
func NewJson(jsonInput string) (Json, error) {
jsonString, err := removeNewLinesFromJSONString(jsonInput)
if err != nil {
return Json(""), err
}
return jsonString, nil
}
func ToJSONStr(JsonAsMap map[string]string) (string, error) {
JsonString, err := json.Marshal(JsonAsMap)
if err != nil {
log.Fatal("error with your json !!!")
}
return string(JsonString), nil
}
func ToJSON(JsonAsMap map[string]string) (Json, error) {
JsonString, err := json.Marshal(JsonAsMap)
if err != nil {
log.Fatal("Error with your json !!!")
}
var JsonJS Json
JsonJS, _ = NewJson(string(JsonString))
return JsonJS, nil
}
func (js *Json) ToMap() (map[string]any, error) {
var jsonMap map[string]any
if err := json.Unmarshal([]byte(*js), &jsonMap); err != nil {
fmt.Println(err)
return nil, err
}
return jsonMap, nil
}
func (js *Json) GetColorizedJSON() (string, error) {
var buf bytes.Buffer
var jsonData []byte = []byte(*js)
// Create a new encoder that writes to the buffer
enc := jsoncolor.NewEncoder(&buf)
// Check if stdout is a color terminal
if jsoncolor.IsColorTerminal(colorable.NewColorableStdout()) {
// Set the colors for the encoder
clrs := &jsoncolor.Colors{
Null: jsoncolor.Color("\x1b[32m"), // Green
Bool: jsoncolor.Color("\x1b[36m"), // Cyan
String: jsoncolor.Color("\x1b[92m"), // Magenta
Number: jsoncolor.Color("\x1b[33m"), // Yellow
Key: jsoncolor.Color("\x1b[94m"), // Red
}
// Apply the colors to the encoder
enc.SetColors(clrs)
}
if isArray(string(jsonData)) {
return encodeArrayOfMaps(jsonData, *enc, &buf)
} else {
return encodeMaps(jsonData, *enc, &buf)
}
}
func encodeMaps(jsonData []byte, enc jsoncolor.Encoder, buf *bytes.Buffer) (string, error) {
var jsonMap map[string]any
// Unmarshal the JSON data
jsonMap, err := ToMapOptionalJS(string(jsonData))
if err != nil {
return "", err
}
enc.SetIndent("", " ")
// Encode the JSON data to the buffer
if err := enc.Encode(jsonMap); err != nil {
return "", err
}
return buf.String(), nil
}
func encodeArrayOfMaps(jsonData []byte, enc jsoncolor.Encoder, buf *bytes.Buffer) (string, error) {
var jsonArray []any
// Unmarshal the JSON data
jsonArray, err := toArrayOfMaps(string(jsonData))
if err != nil {
return "", err
}
enc.SetIndent("", " ")
// Encode the JSON data to the buffer
if err := enc.Encode(jsonArray); err != nil {
return "", err
}
return buf.String(), nil
}
func ToMapOptionalJS(js string) (map[string]any, error) {
var jsonMap map[string]any
if err := json.Unmarshal([]byte(js), &jsonMap); err != nil {
log.Fatal("Error in your json format")
return nil, err
}
return jsonMap, nil
}
func toArrayOfMaps(js string) ([]any, error) {
var arrayOfMaps []any
if err := json.Unmarshal([]byte(js), &arrayOfMaps); err != nil {
return nil, err
}
return arrayOfMaps, nil
}
func isArray(js string) bool {
if string(js[0]) == "[" {
return true
}
return false
}
func removeNewLinesFromJSONString(jsonStr string) (Json, error) {
// parse the JSON string into an interface{}
if string(jsonStr[0]) == "[" {
var jsonArrayOfMaps []any
jsonArrayOfMaps, err := toArrayOfMaps(jsonStr)
_ = jsonArrayOfMaps
if err != nil {
return "", err
}
var modifiedJSONStr []byte
// remove newline characters from all string values recursively
removeNewLinesRecursively(jsonArrayOfMaps)
// encode the modified JSON object back into a string
modifiedJSONStr, err = json.Marshal(jsonArrayOfMaps)
_ = modifiedJSONStr
if err != nil {
return "", err
}
return Json(modifiedJSONStr), nil
} else {
var jsonMap map[string]any
jsonMap, err := ToMapOptionalJS(jsonStr)
if err != nil {
return "", err
}
var modifiedJSONStr []byte
// remove newline characters from all string values recursively
removeNewLinesRecursively(jsonMap)
// encode the modified JSON object back into a string
modifiedJSONStr, err = json.Marshal(jsonMap)
if err != nil {
return "", err
}
// if err != nil {
// }
return Json(modifiedJSONStr), nil
}
}
func removeNewLinesRecursively(jsonObj any) {
switch val := jsonObj.(type) {
case string:
// replace all newline characters in string values
jsonObj = strings.ReplaceAll(val, "\n", "")
case map[string]any:
// traverse map values recursively
for _, v := range val {
removeNewLinesRecursively(v)
}
case []any:
// traverse array values recursively
for _, v := range val {
removeNewLinesRecursively(v)
}
}
}
func IsJson(data string) (bool, error) {
var jsonData any
err := json.Unmarshal([]byte(data), &jsonData)
if err != nil {
fmt.Println(err)
return false, err
} else {
return true, nil
}
}