You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
541 B
Go

package codec
import (
"encoding/json"
"github.com/noahlann/nnet/pkg/codec"
)
// JSONCodec JSON编解码器
type JSONCodec struct{}
// NewJSONCodec 创建JSON编解码器
func NewJSONCodec() codec.Codec {
return &JSONCodec{}
}
// Encode 编码
func (c *JSONCodec) Encode(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// Decode 解码
func (c *JSONCodec) Decode(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
// Name 获取编解码器名称
func (c *JSONCodec) Name() string {
return "json"
}