/
json.go
63 lines (55 loc) · 1.33 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
package outformats
import (
"bufio"
"encoding/json"
"errors"
"io"
)
// JSONWriter JSON format writer
type JSONWriter struct {
fields []string
w *bufio.Writer
}
// Write writes a single JSON record to w one line.
// A record is a slice of strings with each string being one field.
// Writes are buffered, so Flush must eventually be called to ensure
// that the record is written to the underlying io.Writer.
func (w *JSONWriter) Write(records []string) error {
if len(records) != len(w.fields) {
return errors.New("records length is not equal to fields")
}
m := make(map[string]string)
for i := range w.fields {
m[w.fields[i]] = records[i]
}
d, err := json.Marshal(m)
if err != nil {
return err
}
if _, err := w.w.Write(d); err != nil {
return err
}
if _, err := w.w.WriteString("\n"); err != nil {
return err
}
return nil
}
// WriteAll writes multiple json records to w using Write and then calls Flush,
// returning any error from the Flush.
func (w *JSONWriter) WriteAll(records [][]string) error {
for _, record := range records {
err := w.Write(record)
if err != nil {
return err
}
}
return w.w.Flush()
}
// NewJSONWriter generate json writer
// fields are key field
func NewJSONWriter(w io.Writer, fields []string) *JSONWriter {
return &JSONWriter{
w: bufio.NewWriter(w),
fields: fields,
}
}