diff --git a/runtime/marshal_jsonpb.go b/runtime/marshal_jsonpb.go index 3530dddd0aa..2fbb2628727 100644 --- a/runtime/marshal_jsonpb.go +++ b/runtime/marshal_jsonpb.go @@ -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 { diff --git a/runtime/marshal_jsonpb_test.go b/runtime/marshal_jsonpb_test.go index 77da0550b6c..00590e94401 100644 --- a/runtime/marshal_jsonpb_test.go +++ b/runtime/marshal_jsonpb_test.go @@ -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") } @@ -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) } }