2020-11-03 01:27:13 +03:00
|
|
|
// Package json provides a json codec
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2021-09-22 02:05:09 +03:00
|
|
|
"bytes"
|
2020-11-03 01:27:13 +03:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2021-05-25 22:46:40 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
2020-11-03 01:27:13 +03:00
|
|
|
)
|
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
var (
|
|
|
|
DefaultMarshalOptions = JsonMarshalOptions{
|
|
|
|
EscapeHTML: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultUnmarshalOptions = JsonUnmarshalOptions{
|
|
|
|
DisallowUnknownFields: false,
|
|
|
|
UseNumber: false,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
type JsonMarshalOptions struct {
|
|
|
|
EscapeHTML bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type JsonUnmarshalOptions struct {
|
|
|
|
DisallowUnknownFields bool
|
|
|
|
UseNumber bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonCodec struct {
|
|
|
|
opts codec.Options
|
|
|
|
}
|
2020-11-24 16:28:10 +03:00
|
|
|
|
2021-05-25 22:46:40 +03:00
|
|
|
const (
|
|
|
|
flattenTag = "flatten"
|
|
|
|
)
|
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
func (c *jsonCodec) Marshal(v interface{}, opts ...codec.Option) ([]byte, error) {
|
2021-05-26 00:43:38 +03:00
|
|
|
if v == nil {
|
2020-11-29 21:03:18 +03:00
|
|
|
return nil, nil
|
2021-05-26 00:43:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
2020-11-24 16:28:10 +03:00
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:05:09 +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:27:32 +03:00
|
|
|
v = nv
|
2021-05-25 22:46:40 +03:00
|
|
|
}
|
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
marshalOptions := DefaultMarshalOptions
|
|
|
|
if c.opts.Context != nil {
|
|
|
|
if f, ok := c.opts.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok {
|
|
|
|
marshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !marshalOptions.EscapeHTML {
|
|
|
|
w := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetEscapeHTML(marshalOptions.EscapeHTML)
|
|
|
|
err := enc.Encode(v)
|
|
|
|
buf := w.Bytes()
|
|
|
|
return buf[:len(buf)-1], err
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:46:40 +03:00
|
|
|
return json.Marshal(v)
|
2020-11-24 16:28:10 +03:00
|
|
|
}
|
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
func (c *jsonCodec) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
2021-05-26 00:43:38 +03:00
|
|
|
if len(b) == 0 || v == nil {
|
2020-11-24 16:28:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-26 00:43:38 +03:00
|
|
|
|
|
|
|
if m, ok := v.(*codec.Frame); ok {
|
2020-11-24 16:28:10 +03:00
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:05:09 +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:27:32 +03:00
|
|
|
v = nv
|
2021-05-25 22:46:40 +03:00
|
|
|
}
|
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
unmarshalOptions := DefaultUnmarshalOptions
|
|
|
|
if c.opts.Context != nil {
|
|
|
|
if f, ok := c.opts.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok {
|
|
|
|
unmarshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if unmarshalOptions.DisallowUnknownFields || unmarshalOptions.UseNumber {
|
|
|
|
dec := json.NewDecoder(bytes.NewBuffer(b))
|
|
|
|
if unmarshalOptions.DisallowUnknownFields {
|
|
|
|
dec.DisallowUnknownFields()
|
|
|
|
}
|
|
|
|
if unmarshalOptions.UseNumber {
|
|
|
|
dec.UseNumber()
|
|
|
|
}
|
|
|
|
|
|
|
|
return dec.Decode(v)
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:28:10 +03:00
|
|
|
return json.Unmarshal(b, v)
|
2020-11-03 01:27:13 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 23:56:04 +03:00
|
|
|
func (c *jsonCodec) ReadHeader(conn io.Reader, m *codec.Message, t codec.MessageType) error {
|
2020-11-03 01:27:13 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:51:13 +03:00
|
|
|
func (c *jsonCodec) ReadBody(conn io.Reader, v interface{}) error {
|
2021-05-26 00:43:38 +03:00
|
|
|
if v == nil {
|
2020-11-24 16:28:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 00:27:32 +03:00
|
|
|
buf, err := io.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(buf) == 0 {
|
|
|
|
return nil
|
2021-05-25 22:51:13 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:27:32 +03:00
|
|
|
if nv, nerr := rutil.StructFieldByTag(v, codec.DefaultTagName, flattenTag); nerr == nil {
|
|
|
|
v = nv
|
2021-04-12 16:39:16 +03:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:27:32 +03:00
|
|
|
return c.Unmarshal(buf, v)
|
2020-11-03 01:27:13 +03:00
|
|
|
}
|
|
|
|
|
2021-05-25 22:51:13 +03:00
|
|
|
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, v interface{}) error {
|
2021-05-26 00:43:38 +03:00
|
|
|
if v == nil {
|
2020-11-29 21:03:18 +03:00
|
|
|
return nil
|
2021-05-26 00:27:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := c.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-05-25 22:51:13 +03:00
|
|
|
}
|
2021-05-26 00:27:32 +03:00
|
|
|
_, err = conn.Write(buf)
|
2021-05-25 22:51:13 +03:00
|
|
|
return err
|
2020-11-23 18:42:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonCodec) String() string {
|
2020-11-03 01:27:13 +03:00
|
|
|
return "json"
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
func NewCodec(opts ...codec.Option) codec.Codec {
|
|
|
|
return &jsonCodec{opts: codec.NewOptions(opts...)}
|
2020-11-03 01:27:13 +03:00
|
|
|
}
|