2020-11-27 17:43:13 +03:00
|
|
|
// Package json provides a json codec
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/segmentio/encoding/json"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2021-05-26 00:58:47 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
2020-11-27 17:43:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
JsonMarshaler = &Marshaler{}
|
|
|
|
|
|
|
|
JsonUnmarshaler = &Unmarshaler{
|
|
|
|
ZeroCopy: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2020-11-27 17:43:13 +03:00
|
|
|
type Marshaler struct {
|
|
|
|
EscapeHTML bool
|
|
|
|
SortMapKeys bool
|
|
|
|
TrustRawMessage bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Unmarshaler struct {
|
|
|
|
DisallowUnknownFields bool
|
|
|
|
DontCopyNumber bool
|
|
|
|
DontCopyRawMessage bool
|
|
|
|
DontCopyString bool
|
|
|
|
DontMatchCaseInsensitiveStructFields bool
|
|
|
|
UseNumber bool
|
|
|
|
ZeroCopy bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonCodec struct {
|
|
|
|
encodeFlags json.AppendFlags
|
|
|
|
decodeFlags json.ParseFlags
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
func (c *jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:08:49 +03:00
|
|
|
case nil:
|
|
|
|
return nil, nil
|
2020-11-27 17:43:13 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:43:13 +03:00
|
|
|
var err error
|
|
|
|
var buf []byte
|
2021-05-26 00:58:47 +03:00
|
|
|
buf, err = json.Append(buf, v, c.encodeFlags)
|
2020-11-27 17:43:13 +03:00
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
|
2021-04-12 19:17:01 +03:00
|
|
|
if len(b) == 0 {
|
2020-11-27 17:43:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch m := v.(type) {
|
2020-11-29 21:08:49 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-27 17:43:13 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2020-11-27 17:43:13 +03:00
|
|
|
_, err := json.Parse(b, v, c.decodeFlags)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:00:27 +03:00
|
|
|
func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
2020-11-27 17:43:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-11-27 17:43:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(buf) == 0 {
|
2021-04-12 16:43:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 00:58:47 +03:00
|
|
|
return c.Unmarshal(buf, v)
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
|
|
|
if v == nil {
|
2020-11-29 21:08:49 +03:00
|
|
|
return nil
|
2021-05-26 00:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := c.Marshal(v)
|
|
|
|
if err != nil {
|
2020-11-27 17:43:13 +03:00
|
|
|
return err
|
2021-05-26 00:58:47 +03:00
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return codec.ErrInvalidMessage
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
_, err = conn.Write(buf)
|
|
|
|
return err
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonCodec) String() string {
|
|
|
|
return "json"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodec() codec.Codec {
|
|
|
|
var encodeFlags json.AppendFlags
|
|
|
|
var decodeFlags json.ParseFlags
|
|
|
|
|
|
|
|
if JsonMarshaler.EscapeHTML {
|
|
|
|
encodeFlags |= json.EscapeHTML
|
|
|
|
}
|
|
|
|
|
|
|
|
if JsonMarshaler.SortMapKeys {
|
|
|
|
encodeFlags |= json.SortMapKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
if JsonMarshaler.TrustRawMessage {
|
|
|
|
encodeFlags |= json.TrustRawMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
if JsonUnmarshaler.DisallowUnknownFields {
|
|
|
|
decodeFlags |= json.DisallowUnknownFields
|
|
|
|
}
|
|
|
|
|
|
|
|
if JsonUnmarshaler.DontCopyNumber {
|
|
|
|
decodeFlags |= json.DontCopyNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
if JsonUnmarshaler.DontCopyRawMessage {
|
|
|
|
decodeFlags |= json.DontCopyRawMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
if JsonUnmarshaler.DontCopyString {
|
|
|
|
decodeFlags |= json.DontCopyString
|
|
|
|
}
|
|
|
|
|
|
|
|
if JsonUnmarshaler.DontMatchCaseInsensitiveStructFields {
|
|
|
|
decodeFlags |= json.DontMatchCaseInsensitiveStructFields
|
|
|
|
}
|
|
|
|
|
|
|
|
if JsonUnmarshaler.UseNumber {
|
|
|
|
decodeFlags |= json.UseNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
if JsonUnmarshaler.ZeroCopy {
|
|
|
|
decodeFlags |= json.ZeroCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
return &jsonCodec{encodeFlags: encodeFlags, decodeFlags: decodeFlags}
|
|
|
|
}
|