From 58c9a03a2b94322a57a4b56eeb97f8caeaa6f6f2 Mon Sep 17 00:00:00 2001 From: Josh Humphries Date: Thu, 9 May 2019 10:11:53 -0400 Subject: [PATCH 1/3] newline between JSON messages from JSONPb.Encode --- runtime/marshal_jsonpb.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/runtime/marshal_jsonpb.go b/runtime/marshal_jsonpb.go index 3530dddd0aa..0a8e8731305 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([]byte("\n")) + return err + }) } func unmarshalJSONPb(data []byte, v interface{}) error { From a92ddfb03082aa31aabd70a80229a495d6d2d387 Mon Sep 17 00:00:00 2001 From: Josh Humphries Date: Thu, 9 May 2019 12:47:15 -0400 Subject: [PATCH 2/3] use j.Delimiter() --- runtime/marshal_jsonpb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/marshal_jsonpb.go b/runtime/marshal_jsonpb.go index 0a8e8731305..2fbb2628727 100644 --- a/runtime/marshal_jsonpb.go +++ b/runtime/marshal_jsonpb.go @@ -157,7 +157,7 @@ func (j *JSONPb) NewEncoder(w io.Writer) Encoder { } // mimic json.Encoder by adding a newline (makes output // easier to read when it contains multiple encoded items) - _, err := w.Write([]byte("\n")) + _, err := w.Write(j.Delimiter()) return err }) } From 1b30b062c40245c88fcbd0c938eb67f6b08e3e51 Mon Sep 17 00:00:00 2001 From: Josh Humphries Date: Thu, 9 May 2019 12:52:59 -0400 Subject: [PATCH 3/3] fix jsonpb tests --- runtime/marshal_jsonpb_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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) } }