forked from olivere/ndjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
38 lines (32 loc) · 840 Bytes
/
writer.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
package ndjson
import (
"encoding/json"
"io"
)
var (
_ io.Writer = (*Writer)(nil)
)
// Writer implements writing line-oriented JSON data following the
// ndjson spec at http://ndjson.org/.
type Writer struct {
w io.Writer
enc *json.Encoder
}
// NewWriter returns a new Writer, using the underlying writer for output.
func NewWriter(w io.Writer) *Writer {
return &Writer{
w: w,
enc: json.NewEncoder(w),
}
}
// Write writes the contents of p into the buffer. It returns the number of
// bytes written. if n < len(p), it also returns an error explaining why
// the write is short.
func (w *Writer) Write(p []byte) (n int, err error) {
return w.w.Write(p)
}
// Encode encodes v with a JSON encoder and writes the output to the
// underlying writer.
func (w *Writer) Encode(v interface{}) error {
return w.enc.Encode(v)
}