2019-01-01 01:01:16 +03:00
|
|
|
// Package json provides a json codec
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/codec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Codec struct {
|
|
|
|
Conn io.ReadWriteCloser
|
|
|
|
Encoder *json.Encoder
|
|
|
|
Decoder *json.Decoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Codec) ReadHeader(m *codec.Message, t codec.MessageType) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Codec) ReadBody(b interface{}) error {
|
2019-01-11 01:14:32 +03:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-01 01:01:16 +03:00
|
|
|
return c.Decoder.Decode(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Codec) Write(m *codec.Message, b interface{}) error {
|
2019-01-11 01:14:32 +03:00
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-01 01:01:16 +03:00
|
|
|
return c.Encoder.Encode(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Codec) Close() error {
|
|
|
|
return c.Conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Codec) String() string {
|
|
|
|
return "json"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodec(c io.ReadWriteCloser) codec.Codec {
|
|
|
|
return &Codec{
|
|
|
|
Conn: c,
|
|
|
|
Decoder: json.NewDecoder(c),
|
|
|
|
Encoder: json.NewEncoder(c),
|
|
|
|
}
|
|
|
|
}
|