86 lines
1.4 KiB
Go
86 lines
1.4 KiB
Go
// Package json provides a json codec
|
|
package json
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"io/ioutil"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
)
|
|
|
|
type jsonCodec struct{}
|
|
|
|
func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) {
|
|
switch m := b.(type) {
|
|
case nil:
|
|
return nil, nil
|
|
case *codec.Frame:
|
|
return m.Data, nil
|
|
}
|
|
|
|
return json.Marshal(b)
|
|
}
|
|
|
|
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
|
|
if len(b) == 0 {
|
|
return nil
|
|
}
|
|
switch m := v.(type) {
|
|
case nil:
|
|
return nil
|
|
case *codec.Frame:
|
|
m.Data = b
|
|
return nil
|
|
}
|
|
|
|
return json.Unmarshal(b, v)
|
|
}
|
|
|
|
func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
|
return nil
|
|
}
|
|
|
|
func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error {
|
|
switch m := b.(type) {
|
|
case nil:
|
|
return nil
|
|
case *codec.Frame:
|
|
buf, err := ioutil.ReadAll(conn)
|
|
if err != nil {
|
|
return err
|
|
} else if len(buf) == 0 {
|
|
return nil
|
|
}
|
|
m.Data = buf
|
|
return nil
|
|
}
|
|
|
|
err := json.NewDecoder(conn).Decode(b)
|
|
if err == io.EOF {
|
|
return nil
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
|
switch m := b.(type) {
|
|
case nil:
|
|
return nil
|
|
case *codec.Frame:
|
|
_, err := conn.Write(m.Data)
|
|
return err
|
|
}
|
|
|
|
return json.NewEncoder(conn).Encode(b)
|
|
}
|
|
|
|
func (c *jsonCodec) String() string {
|
|
return "json"
|
|
}
|
|
|
|
func NewCodec() codec.Codec {
|
|
return &jsonCodec{}
|
|
}
|