This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ndjson.go
79 lines (70 loc) · 1.42 KB
/
ndjson.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
package io
import (
"bufio"
gio "github.com/gogo/protobuf/io"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"io"
"strings"
)
func NewNdjsonWriter(w io.Writer) gio.WriteCloser {
return &ndjsonWriter{w}
}
type ndjsonWriter struct {
w io.Writer
}
func (this *ndjsonWriter) WriteMsg(msg proto.Message) (err error) {
ma := jsonpb.Marshaler{
EnumsAsInts: false,
EmitDefaults: false,
}
msgJson, err := ma.MarshalToString(msg)
if err != nil {
return err
}
out := bufio.NewWriter(this.w)
_, err = out.WriteString(msgJson)
if err != nil {
return err
}
_, err = out.WriteString("\n")
if err != nil {
return err
}
err = out.Flush()
if err != nil {
return err
}
return nil
}
func (this *ndjsonWriter) Close() error {
if closer, ok := this.w.(io.Closer); ok {
return closer.Close()
}
return nil
}
func NewNdjsonReader(r io.Reader) gio.ReadCloser {
var closer io.Closer
if c, ok := r.(io.Closer); ok {
closer = c
}
scanner := bufio.NewScanner(r)
return &ndjsonReader{bufio.NewReader(r), scanner, closer}
}
type ndjsonReader struct {
r *bufio.Reader
scanner *bufio.Scanner
closer io.Closer
}
func (this *ndjsonReader) ReadMsg(msg proto.Message) error {
if this.scanner.Scan() {
return jsonpb.Unmarshal(strings.NewReader(this.scanner.Text()), msg)
}
return io.EOF
}
func (this *ndjsonReader) Close() error {
if this.closer != nil {
return this.closer.Close()
}
return nil
}