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 (
|
2021-09-24 00:45:52 +03:00
|
|
|
DefaultMarshalOptions = JsonMarshalOptions{
|
|
|
|
EscapeHTML: true,
|
|
|
|
SortMapKeys: true,
|
|
|
|
}
|
2020-11-27 17:43:13 +03:00
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
DefaultUnmarshalOptions = JsonUnmarshalOptions{
|
2020-11-27 17:43:13 +03:00
|
|
|
ZeroCopy: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
var _ codec.Codec = &jsonCodec{}
|
|
|
|
|
2021-05-26 00:58:47 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
type JsonMarshalOptions struct {
|
2020-11-27 17:43:13 +03:00
|
|
|
EscapeHTML bool
|
|
|
|
SortMapKeys bool
|
|
|
|
TrustRawMessage bool
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
type JsonUnmarshalOptions struct {
|
2020-11-27 17:43:13 +03:00
|
|
|
DisallowUnknownFields bool
|
|
|
|
DontCopyNumber bool
|
|
|
|
DontCopyRawMessage bool
|
|
|
|
DontCopyString bool
|
|
|
|
DontMatchCaseInsensitiveStructFields bool
|
|
|
|
UseNumber bool
|
|
|
|
ZeroCopy bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonCodec struct {
|
2021-09-24 00:45:52 +03:00
|
|
|
opts codec.Options
|
2020-11-27 17:43:13 +03:00
|
|
|
encodeFlags json.AppendFlags
|
|
|
|
decodeFlags json.ParseFlags
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
func getMarshalFlags(o JsonMarshalOptions) json.AppendFlags {
|
|
|
|
var encodeFlags json.AppendFlags
|
|
|
|
|
|
|
|
if o.EscapeHTML {
|
|
|
|
encodeFlags |= json.EscapeHTML
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.SortMapKeys {
|
|
|
|
encodeFlags |= json.SortMapKeys
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.TrustRawMessage {
|
|
|
|
encodeFlags |= json.TrustRawMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodeFlags
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUnmarshalFlags(o JsonUnmarshalOptions) json.ParseFlags {
|
|
|
|
var decodeFlags json.ParseFlags
|
|
|
|
|
|
|
|
if o.DisallowUnknownFields {
|
|
|
|
decodeFlags |= json.DisallowUnknownFields
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.DontCopyNumber {
|
|
|
|
decodeFlags |= json.DontCopyNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.DontCopyRawMessage {
|
|
|
|
decodeFlags |= json.DontCopyRawMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.DontCopyString {
|
|
|
|
decodeFlags |= json.DontCopyString
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.DontMatchCaseInsensitiveStructFields {
|
|
|
|
decodeFlags |= json.DontMatchCaseInsensitiveStructFields
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.UseNumber {
|
|
|
|
decodeFlags |= json.UseNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.ZeroCopy {
|
|
|
|
decodeFlags |= json.ZeroCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodeFlags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
|
|
|
if v == nil {
|
2020-11-29 21:08:49 +03:00
|
|
|
return nil, nil
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
|
2021-05-26 00:58:47 +03:00
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
marshalOptions := DefaultMarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok {
|
|
|
|
marshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 17:43:13 +03:00
|
|
|
var err error
|
|
|
|
var buf []byte
|
2021-09-24 00:45:52 +03:00
|
|
|
buf, err = json.Append(buf, v, getMarshalFlags(marshalOptions))
|
2020-11-27 17:43:13 +03:00
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
|
|
|
if len(b) == 0 || v == nil {
|
2020-11-27 17:43:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-24 00:45:52 +03:00
|
|
|
|
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, flattenTag); err == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
|
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
2020-11-27 17:43:13 +03:00
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
unmarshalOptions := DefaultUnmarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok {
|
|
|
|
unmarshalOptions = f
|
|
|
|
}
|
2021-05-26 00:58:47 +03:00
|
|
|
}
|
2021-09-24 00:45:52 +03:00
|
|
|
|
|
|
|
_, err := json.Parse(b, v, getUnmarshalFlags(unmarshalOptions))
|
2020-11-27 17:43:13 +03:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:45:52 +03:00
|
|
|
func NewCodec(opts ...codec.Option) *jsonCodec {
|
|
|
|
return &jsonCodec{opts: codec.NewOptions(opts...)}
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|