2020-11-03 01:27:13 +03:00
|
|
|
// Package json provides a json codec
|
2024-09-17 00:38:06 +03:00
|
|
|
package json
|
2020-11-03 01:27:13 +03:00
|
|
|
|
|
|
|
import (
|
2021-09-22 02:05:09 +03:00
|
|
|
"bytes"
|
2020-11-03 01:27:13 +03:00
|
|
|
"encoding/json"
|
2024-09-17 00:38:06 +03:00
|
|
|
"reflect"
|
2020-11-03 01:27:13 +03:00
|
|
|
|
2023-03-05 21:58:01 +03:00
|
|
|
pb "go.unistack.org/micro-proto/v3/codec"
|
2021-10-03 18:43:19 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
rutil "go.unistack.org/micro/v3/util/reflect"
|
2020-11-03 01:27:13 +03:00
|
|
|
)
|
|
|
|
|
2024-09-17 00:38:06 +03:00
|
|
|
var _ codec.Codec = (*jsonCodec)(nil)
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2024-09-17 00:38:06 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-05-25 22:46:40 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 21:58:01 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
2021-09-24 00:00:41 +03:00
|
|
|
return m.Data, nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
marshalOptions := DefaultMarshalOptions
|
2021-09-22 02:14:12 +03:00
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok {
|
2021-09-22 02:05:09 +03:00
|
|
|
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
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2024-09-17 00:38:06 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
|
|
|
v = nv
|
|
|
|
rv := reflect.ValueOf(v)
|
|
|
|
if rv.Kind() != reflect.Pointer &&
|
|
|
|
rv.Kind() != reflect.Map {
|
|
|
|
v = reflect.New(rv.Type()).Interface()
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 22:46:40 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 21:58:01 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
2021-09-24 00:00:41 +03:00
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-22 02:05:09 +03:00
|
|
|
unmarshalOptions := DefaultUnmarshalOptions
|
2021-09-22 02:14:12 +03:00
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok {
|
2021-09-22 02:05:09 +03:00
|
|
|
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-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
|
|
|
}
|