-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
113 lines (103 loc) · 2.75 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
package main
import (
"encoding/json"
"fmt"
"io"
)
// JSONFormatter is a table formatter that prints nd-json lines; if extended
// format, records are splayed across one line per field and wrapped in a
// top-level array with commas (actual JSON); if normal, one line per record
// without commas (ND-JSON).
type JSONFormatter struct {
Output io.Writer
ExtendedFormat bool
headers []string
started bool
comma bool
enc *json.Encoder
}
func (j *JSONFormatter) SetColumnNames(headers []string) {
j.headers = headers
}
func (j *JSONFormatter) AddRow(row []string) {
rl := len(row)
if rl != len(j.headers) {
panic(fmt.Sprintf("internal error: headers has %d fields, AddRow() has %d fields", len(j.headers), len(row)))
}
if !j.started {
j.started = true
j.start()
}
if j.ExtendedFormat {
// I want careful control over formatting/field ordering of the extended format,
// so manually generate it. Because it's just strings, and we already quoted them,
// it's still syntactically safe.
if j.comma {
j.Output.Write(jsonExtendedComma)
} else {
j.comma = true
}
j.Output.Write(jsonExtendedRecordStart)
for i, h := range j.headers {
j.Output.Write(jsonExtendedRecordFieldStart)
j.enc.Encode(h)
j.Output.Write(jsonExtendedRecordValueStart)
j.enc.Encode(row[i])
if i+1 != rl {
j.Output.Write(jsonExtendedRecordValueEnd)
}
}
j.Output.Write(jsonExtendedRecordEnd)
} else {
// build a map, jam the data into it, let the encoder sort it out
m := map[string]string{}
for i, h := range j.headers {
m[h] = row[i]
}
j.enc.Encode(m)
}
}
func (j *JSONFormatter) Close() error {
j.stop()
return nil
}
var jsonExtendedStart = []byte{'[', '\n'}
var jsonExtendedRecordStart = []byte{'{', '\n'}
var jsonExtendedRecordFieldStart = []byte{' ', ' '}
var jsonExtendedRecordValueStart = []byte{':', ' '}
var jsonExtendedRecordValueEnd = []byte{',', '\n'}
var jsonExtendedRecordEnd = []byte{'\n', '}'}
var jsonExtendedComma = []byte{',', '\n'}
var jsonExtendedEnd = []byte{'\n', ']', '\n'}
// Strip newlined from the end of writing, to adapt the JSON encoder which puts
// them there after each thing it writes.
type writerWithoutNewline struct {
w io.Writer
}
func (w writerWithoutNewline) Write(b []byte) (int, error) {
l := len(b)
if l > 0 && b[l-1] == '\n' {
if l == 1 {
return 1, nil
}
i, j := w.w.Write(b[:l-1])
if j != nil {
return i, j
}
return i + 1, nil
}
return w.w.Write(b)
}
func (j *JSONFormatter) start() {
if j.ExtendedFormat {
j.Output.Write(jsonExtendedStart)
j.enc = json.NewEncoder(writerWithoutNewline{j.Output})
} else {
j.enc = json.NewEncoder(j.Output)
}
}
func (j *JSONFormatter) stop() {
if j.ExtendedFormat {
j.Output.Write(jsonExtendedEnd)
}
}