forked from TylerBrock/colorjson
/
colorjson.go
200 lines (175 loc) · 4.89 KB
/
colorjson.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
package colorjson
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"github.com/fatih/color"
)
const initialDepth = 0
const valueSep = ","
const null = "null"
const startMap = "{"
const endMap = "}"
const startArray = "["
const endArray = "]"
const emptyMap = startMap + endMap
const emptyArray = startArray + endArray
type Formatter struct {
KeyColor *color.Color
StringColor *color.Color
BoolColor *color.Color
NumberColor *color.Color
NullColor *color.Color
StringMaxLength int
Indent int
DisabledColor bool
RawStrings bool
}
func NewFormatter() *Formatter {
return &Formatter{
KeyColor: color.New(color.FgWhite),
StringColor: color.New(color.FgGreen),
BoolColor: color.New(color.FgYellow),
NumberColor: color.New(color.FgCyan),
NullColor: color.New(color.FgMagenta),
StringMaxLength: 0,
DisabledColor: false,
Indent: 0,
RawStrings: false,
}
}
func (f *Formatter) sprintfColor(c *color.Color, format string, args ...interface{}) string {
if f.DisabledColor || c == nil {
return fmt.Sprintf(format, args...)
}
return c.SprintfFunc()(format, args...)
}
func (f *Formatter) sprintColor(c *color.Color, s string) string {
if f.DisabledColor || c == nil {
return fmt.Sprint(s)
}
return c.SprintFunc()(s)
}
func (f *Formatter) writeIndent(buf *bytes.Buffer, depth int) {
buf.WriteString(strings.Repeat(" ", f.Indent*depth))
}
func (f *Formatter) writeObjSep(buf *bytes.Buffer) {
if f.Indent != 0 {
buf.WriteByte('\n')
} else {
buf.WriteByte(' ')
}
}
func (f *Formatter) Marshal(jsonObj interface{}) ([]byte, error) {
buffer := bytes.Buffer{}
f.marshalValue(jsonObj, &buffer, initialDepth)
return buffer.Bytes(), nil
}
func (f *Formatter) marshalMap(m map[string]interface{}, buf *bytes.Buffer, depth int) {
remaining := len(m)
if remaining == 0 {
buf.WriteString(emptyMap)
return
}
keys := make([]string, 0)
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
buf.WriteString(startMap)
f.writeObjSep(buf)
for _, key := range keys {
f.writeIndent(buf, depth+1)
buf.WriteString(f.KeyColor.Sprintf("\"%s\": ", key))
f.marshalValue(m[key], buf, depth+1)
remaining--
if remaining != 0 {
buf.WriteString(valueSep)
}
f.writeObjSep(buf)
}
f.writeIndent(buf, depth)
buf.WriteString(endMap)
}
func (f *Formatter) marshalArray(a []interface{}, buf *bytes.Buffer, depth int) {
if len(a) == 0 {
buf.WriteString(emptyArray)
return
}
buf.WriteString(startArray)
f.writeObjSep(buf)
for i, v := range a {
f.writeIndent(buf, depth+1)
f.marshalValue(v, buf, depth+1)
if i < len(a)-1 {
buf.WriteString(valueSep)
}
f.writeObjSep(buf)
}
f.writeIndent(buf, depth)
buf.WriteString(endArray)
}
func (f *Formatter) marshalValue(val interface{}, buf *bytes.Buffer, depth int) {
switch v := val.(type) {
case map[string]interface{}:
f.marshalMap(v, buf, depth)
case []interface{}:
f.marshalArray(v, buf, depth)
case string:
f.marshalString(v, buf)
case []string:
buf.WriteString("[")
for i, s := range v {
f.marshalString(s, buf)
if i != len(v) - 1 {
buf.WriteString(", ")
}
}
buf.WriteString("]")
case int:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(int64(v), 10)))
case int8:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(int64(v), 10)))
case int16:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(int64(v), 10)))
case int32:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(int64(v), 10)))
case int64:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(v, 10)))
case uint:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(int64(v), 10)))
case uint8:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(int64(v), 10)))
case uint16:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(int64(v), 10)))
case uint32:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(int64(v), 10)))
case uint64:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatInt(int64(v), 10)))
case float32:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatFloat(float64(v), 'f', -1, 64)))
case float64:
buf.WriteString(f.sprintColor(f.NumberColor, strconv.FormatFloat(v, 'f', -1, 64)))
case bool:
buf.WriteString(f.sprintColor(f.BoolColor, strconv.FormatBool(v)))
case nil:
buf.WriteString(f.sprintColor(f.NullColor, null))
}
}
func (f *Formatter) marshalString(str string, buf *bytes.Buffer) {
if !f.RawStrings {
strBytes, _ := json.Marshal(str)
str = string(strBytes)
}
if f.StringMaxLength != 0 && len(str) >= f.StringMaxLength {
str = fmt.Sprintf("%s...", str[0:f.StringMaxLength])
}
buf.WriteString(f.sprintColor(f.StringColor, str))
}
// Marshal JSON data with default options
func Marshal(jsonObj interface{}) ([]byte, error) {
return NewFormatter().Marshal(jsonObj)
}