From cd2e0c6b3b439658fa01f8962c50fea1c17d3a5c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 25 May 2021 22:51:13 +0300 Subject: [PATCH] fixup Signed-off-by: Vasiliy Tolstov --- json.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/json.go b/json.go index d7d805d..f76009e 100644 --- a/json.go +++ b/json.go @@ -53,8 +53,8 @@ func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.Message return nil } -func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error { - switch m := b.(type) { +func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error { + switch m := v.(type) { case nil: return nil case *codec.Frame: @@ -68,7 +68,13 @@ func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error { return nil } - err := json.NewDecoder(conn).Decode(b) + var err error + if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil { + err = json.NewDecoder(conn).Decode(nv) + } else { + err = json.NewDecoder(conn).Decode(v) + } + if err == io.EOF { return nil } @@ -76,8 +82,8 @@ func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error { return err } -func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error { - switch m := b.(type) { +func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error { + switch m := v.(type) { case nil: return nil case *codec.Frame: @@ -85,7 +91,14 @@ func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error return err } - return json.NewEncoder(conn).Encode(b) + var err error + if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil { + err = json.NewEncoder(conn).Encode(nv) + } else { + err = json.NewEncoder(conn).Encode(v) + } + + return err } func (c *jsonCodec) String() string {