-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathprettyjson.go
200 lines (164 loc) · 5.05 KB
/
prettyjson.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 prettyjson provides JSON pretty print.
package prettyjson
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
"github.com/fatih/color"
)
// Formatter is a struct to format JSON data. `color` is github.com/fatih/color: https://github.com/fatih/color
type Formatter struct {
// JSON key color. Default is `color.New(color.FgBlue, color.Bold)`.
KeyColor *color.Color
// JSON string value color. Default is `color.New(color.FgGreen, color.Bold)`.
StringColor *color.Color
// JSON boolean value color. Default is `color.New(color.FgYellow, color.Bold)`.
BoolColor *color.Color
// JSON number value color. Default is `color.New(color.FgCyan, color.Bold)`.
NumberColor *color.Color
// JSON null value color. Default is `color.New(color.FgBlack, color.Bold)`.
NullColor *color.Color
// Max length of JSON string value. When the value is 1 and over, string is truncated to length of the value.
// Default is 0 (not truncated).
StringMaxLength int
// Boolean to disable color. Default is false.
DisabledColor bool
// Indent space number. Default is 2.
Indent int
// Newline string. To print without new lines set it to empty string. Default is \n.
Newline string
}
// NewFormatter returns a new formatter with following default values.
func NewFormatter() *Formatter {
return &Formatter{
KeyColor: color.New(color.FgBlue, color.Bold),
StringColor: color.New(color.FgGreen, color.Bold),
BoolColor: color.New(color.FgYellow, color.Bold),
NumberColor: color.New(color.FgCyan, color.Bold),
NullColor: color.New(color.FgBlack, color.Bold),
StringMaxLength: 0,
DisabledColor: false,
Indent: 2,
Newline: "\n",
}
}
// Marshal marshals and formats JSON data.
func (f *Formatter) Marshal(v interface{}) ([]byte, error) {
data, err := json.Marshal(v)
if err != nil {
return nil, err
}
return f.Format(data)
}
// Format formats JSON string.
func (f *Formatter) Format(data []byte) ([]byte, error) {
var v interface{}
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()
if err := decoder.Decode(&v); err != nil {
return nil, err
}
return []byte(f.pretty(v, 1)), nil
}
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) pretty(v interface{}, depth int) string {
switch val := v.(type) {
case string:
return f.processString(val)
case float64:
return f.sprintColor(f.NumberColor, strconv.FormatFloat(val, 'f', -1, 64))
case json.Number:
return f.sprintColor(f.NumberColor, string(val))
case bool:
return f.sprintColor(f.BoolColor, strconv.FormatBool(val))
case nil:
return f.sprintColor(f.NullColor, "null")
case map[string]interface{}:
return f.processMap(val, depth)
case []interface{}:
return f.processArray(val, depth)
}
return ""
}
func (f *Formatter) processString(s string) string {
r := []rune(s)
if f.StringMaxLength != 0 && len(r) >= f.StringMaxLength {
s = string(r[0:f.StringMaxLength]) + "..."
}
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
encoder.Encode(s)
s = string(buf.Bytes())
s = strings.TrimSuffix(s, "\n")
return f.sprintColor(f.StringColor, s)
}
func (f *Formatter) processMap(m map[string]interface{}, depth int) string {
if len(m) == 0 {
return "{}"
}
currentIndent := f.generateIndent(depth - 1)
nextIndent := f.generateIndent(depth)
rows := []string{}
keys := []string{}
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
val := m[key]
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
encoder.Encode(key)
s := strings.TrimSuffix(string(buf.Bytes()), "\n")
k := f.sprintColor(f.KeyColor, s)
v := f.pretty(val, depth+1)
valueIndent := " "
if f.Newline == "" {
valueIndent = ""
}
row := fmt.Sprintf("%s%s:%s%s", nextIndent, k, valueIndent, v)
rows = append(rows, row)
}
return fmt.Sprintf("{%s%s%s%s}", f.Newline, strings.Join(rows, ","+f.Newline), f.Newline, currentIndent)
}
func (f *Formatter) processArray(a []interface{}, depth int) string {
if len(a) == 0 {
return "[]"
}
currentIndent := f.generateIndent(depth - 1)
nextIndent := f.generateIndent(depth)
rows := []string{}
for _, val := range a {
c := f.pretty(val, depth+1)
row := nextIndent + c
rows = append(rows, row)
}
return fmt.Sprintf("[%s%s%s%s]", f.Newline, strings.Join(rows, ","+f.Newline), f.Newline, currentIndent)
}
func (f *Formatter) generateIndent(depth int) string {
return strings.Repeat(" ", f.Indent*depth)
}
// Marshal JSON data with default options.
func Marshal(v interface{}) ([]byte, error) {
return NewFormatter().Marshal(v)
}
// Format JSON string with default options.
func Format(data []byte) ([]byte, error) {
return NewFormatter().Format(data)
}