2020-11-03 01:27:13 +03:00
|
|
|
// Package json provides a json codec
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2021-05-25 22:46:40 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
2020-11-03 01:27:13 +03:00
|
|
|
)
|
|
|
|
|
2020-11-24 16:28:10 +03:00
|
|
|
type jsonCodec struct{}
|
|
|
|
|
2021-05-25 22:46:40 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:03:18 +03:00
|
|
|
case nil:
|
|
|
|
return nil, nil
|
2020-11-24 16:28:10 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:46:40 +03:00
|
|
|
if nv, err := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); err == nil {
|
|
|
|
return json.Marshal(nv)
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(v)
|
2020-11-24 16:28:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
|
2021-04-12 19:12:16 +03:00
|
|
|
if len(b) == 0 {
|
2020-11-24 16:28:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:03:18 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-24 16:28:10 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:46:40 +03:00
|
|
|
if nv, err := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); err == nil {
|
|
|
|
return json.Unmarshal(b, nv)
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:28:10 +03:00
|
|
|
return json.Unmarshal(b, v)
|
2020-11-03 01:27:13 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 23:56:04 +03:00
|
|
|
func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
2020-11-03 01:27:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-20 23:56:04 +03:00
|
|
|
func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error {
|
2020-11-24 16:28:10 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:03:18 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-24 16:28:10 +03:00
|
|
|
case *codec.Frame:
|
2021-05-25 22:46:40 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
2020-11-24 16:28:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-12 19:12:16 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return nil
|
2020-11-24 16:28:10 +03:00
|
|
|
}
|
|
|
|
m.Data = buf
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-12 16:39:16 +03:00
|
|
|
err := json.NewDecoder(conn).Decode(b)
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2020-11-03 01:27:13 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 23:56:04 +03:00
|
|
|
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
2020-11-24 16:28:10 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:03:18 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-24 16:28:10 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
_, err := conn.Write(m.Data)
|
|
|
|
return err
|
2020-11-23 18:42:17 +03:00
|
|
|
}
|
2020-11-03 01:27:13 +03:00
|
|
|
|
2020-11-24 16:28:10 +03:00
|
|
|
return json.NewEncoder(conn).Encode(b)
|
2020-11-23 18:42:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonCodec) String() string {
|
2020-11-03 01:27:13 +03:00
|
|
|
return "json"
|
|
|
|
}
|
|
|
|
|
2020-11-23 18:42:17 +03:00
|
|
|
func NewCodec() codec.Codec {
|
|
|
|
return &jsonCodec{}
|
2020-11-03 01:27:13 +03:00
|
|
|
}
|