2020-11-27 17:43:13 +03:00
|
|
|
// Package json provides a json codec
|
2021-10-25 20:39:17 +03:00
|
|
|
package json // import "go.unistack.org/micro-codec-segmentio/v3/json"
|
2020-11-27 17:43:13 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/segmentio/encoding/json"
|
2023-03-05 22:25:51 +03:00
|
|
|
pb "go.unistack.org/micro-proto/v3/codec"
|
2021-10-25 20:24:40 +03:00
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
rutil "go.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,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
var (
|
2024-09-17 13:03:21 +03:00
|
|
|
_ codec.Codec = (*jsonCodec)(nil)
|
|
|
|
_ codec.CodecV2 = (*jsonCodecV2)(nil)
|
2021-05-26 00:58:47 +03:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
type jsonCodecV2 struct {
|
|
|
|
opts codec.Options
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-05-26 00:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:25:51 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
2021-09-24 00:45:52 +03:00
|
|
|
return m.Data, nil
|
2024-12-23 20:46:53 +03:00
|
|
|
case codec.RawMessage:
|
|
|
|
return []byte(m), nil
|
|
|
|
case *codec.RawMessage:
|
|
|
|
return []byte(*m), nil
|
2021-09-24 00:45:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
if options.Flatten {
|
|
|
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2021-09-24 00:45:52 +03:00
|
|
|
}
|
|
|
|
|
2023-03-05 22:25:51 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
2020-11-27 17:43:13 +03:00
|
|
|
m.Data = b
|
|
|
|
return nil
|
2024-12-23 20:46:53 +03:00
|
|
|
case *codec.RawMessage:
|
|
|
|
*m = append((*m)[0:0], b...)
|
|
|
|
return nil
|
|
|
|
case codec.RawMessage:
|
|
|
|
copy(m, b)
|
|
|
|
return nil
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
func (c *jsonCodec) String() string {
|
|
|
|
return "json"
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
func (c *jsonCodecV2) Marshal(b []byte, v interface{}, opts ...codec.Option) ([]byte, error) {
|
2021-05-26 00:58:47 +03:00
|
|
|
if v == nil {
|
2024-09-17 12:28:35 +03:00
|
|
|
return nil, nil
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Flatten {
|
|
|
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
return m.Data, nil
|
|
|
|
case *pb.Frame:
|
|
|
|
return m.Data, nil
|
2021-04-12 16:43:49 +03:00
|
|
|
}
|
2024-09-17 12:28:35 +03:00
|
|
|
|
|
|
|
marshalOptions := DefaultMarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(marshalOptionsKey{}).(JsonMarshalOptions); ok {
|
|
|
|
marshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2024-09-17 17:36:45 +03:00
|
|
|
b, err = json.Append(b[:0], v, getMarshalFlags(marshalOptions))
|
2024-09-17 12:28:35 +03:00
|
|
|
return b, err
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
func (c *jsonCodecV2) Unmarshal(b []byte, v interface{}, opts ...codec.Option) error {
|
|
|
|
if len(b) == 0 || v == nil {
|
2020-11-29 21:08:49 +03:00
|
|
|
return nil
|
2021-05-26 00:58:47 +03:00
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
options := c.opts
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Flatten {
|
|
|
|
if nv, err := rutil.StructFieldByTag(v, options.TagName, "flatten"); err == nil {
|
|
|
|
v = nv
|
|
|
|
}
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
switch m := v.(type) {
|
|
|
|
case *codec.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
case *pb.Frame:
|
|
|
|
m.Data = b
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
unmarshalOptions := DefaultUnmarshalOptions
|
|
|
|
if options.Context != nil {
|
|
|
|
if f, ok := options.Context.Value(unmarshalOptionsKey{}).(JsonUnmarshalOptions); ok {
|
|
|
|
unmarshalOptions = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := json.Parse(b, v, getUnmarshalFlags(unmarshalOptions))
|
2021-05-26 00:58:47 +03:00
|
|
|
return err
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2024-09-17 12:28:35 +03:00
|
|
|
func (c *jsonCodecV2) String() string {
|
2020-11-27 17:43:13 +03:00
|
|
|
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
|
|
|
}
|
2024-09-17 12:31:43 +03:00
|
|
|
|
|
|
|
func NewCodecV2(opts ...codec.Option) *jsonCodecV2 {
|
|
|
|
return &jsonCodecV2{opts: codec.NewOptions(opts...)}
|
|
|
|
}
|