forked from gazette/core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
json_framing.go
37 lines (29 loc) · 949 Bytes
/
json_framing.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
package message
import (
"bufio"
"encoding/json"
"github.com/LiveRamp/gazette/v2/pkg/labels"
)
// JSONFraming is a Framing implementation which encodes messages as line-
// delimited JSON. Messages must be encode-able by the encoding/json package.
var JSONFraming = new(jsonFraming)
type jsonFraming struct{}
// ContentType returns labels.ContentType_JSONLines.
func (*jsonFraming) ContentType() string { return labels.ContentType_JSONLines }
// Marshal implements Framing.
func (*jsonFraming) Marshal(msg Message, bw *bufio.Writer) error {
return json.NewEncoder(bw).Encode(msg)
}
// Unpack implements Framing.
func (*jsonFraming) Unpack(r *bufio.Reader) ([]byte, error) {
return UnpackLine(r)
}
// Unmarshal implements Framing.
func (*jsonFraming) Unmarshal(line []byte, msg Message) error {
if err := json.Unmarshal(line, msg); err != nil {
return err
} else if f, ok := msg.(Fixupable); ok {
return f.Fixup()
}
return nil
}