Skip to content

Commit

Permalink
newline between JSON messages (#931)
Browse files Browse the repository at this point in the history
* newline between JSON messages from JSONPb.Encode

* use j.Delimiter()

* fix jsonpb tests
  • Loading branch information
jhump authored and johanbrandhorst committed May 10, 2019
1 parent f005bec commit e178b56
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 9 additions & 1 deletion runtime/marshal_jsonpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ func (d DecoderWrapper) Decode(v interface{}) error {

// NewEncoder returns an Encoder which writes JSON stream into "w".
func (j *JSONPb) NewEncoder(w io.Writer) Encoder {
return EncoderFunc(func(v interface{}) error { return j.marshalTo(w, v) })
return EncoderFunc(func(v interface{}) error {
if err := j.marshalTo(w, v); err != nil {
return err
}
// mimic json.Encoder by adding a newline (makes output
// easier to read when it contains multiple encoded items)
_, err := w.Write(j.Delimiter())
return err
})
}

func unmarshalJSONPb(data []byte, v interface{}) error {
Expand Down
5 changes: 4 additions & 1 deletion runtime/marshal_jsonpb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ func TestJSONPbEncoder(t *testing.T) {
}{
{
verifier: func(json string) {
// remove trailing delimiter before verifying
json = strings.TrimSuffix(json, "\n")

if strings.ContainsAny(json, " \t\r\n") {
t.Errorf("strings.ContainsAny(%q, %q) = true; want false", json, " \t\r\n")
}
Expand Down Expand Up @@ -356,7 +359,7 @@ func TestJSONPbEncoderFields(t *testing.T) {
if err := enc.Encode(fixt.data); err != nil {
t.Errorf("enc.Encode(%#v) failed with %v; want success", fixt.data, err)
}
if got, want := buf.String(), fixt.json; got != want {
if got, want := buf.String(), fixt.json + string(m.Delimiter()); got != want {
t.Errorf("enc.Encode(%#v) = %q; want %q", fixt.data, got, want)
}
}
Expand Down

0 comments on commit e178b56

Please sign in to comment.