2020-11-27 17:43:13 +03:00
|
|
|
// Package json provides a json codec
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/segmentio/encoding/json"
|
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
JsonMarshaler = &Marshaler{}
|
|
|
|
|
|
|
|
JsonUnmarshaler = &Unmarshaler{
|
|
|
|
ZeroCopy: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonCodec) Marshal(b interface{}) ([]byte, error) {
|
|
|
|
switch m := b.(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
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var buf []byte
|
|
|
|
buf, err = json.Append(buf, b, c.encodeFlags)
|
|
|
|
return buf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *jsonCodec) Unmarshal(b []byte, v interface{}) error {
|
2020-11-29 21:08:49 +03:00
|
|
|
if b == nil {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
|
2020-12-21 00:00:27 +03:00
|
|
|
func (c *jsonCodec) ReadBody(conn io.Reader, b interface{}) error {
|
2020-11-27 17:43:13 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:08:49 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-27 17:43:13 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
buf, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Data = buf
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-12 16:43:49 +03:00
|
|
|
err := json.NewDecoder(conn).Decode(b)
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2020-11-27 17:43:13 +03:00
|
|
|
}
|
|
|
|
|
2020-12-21 00:00:27 +03:00
|
|
|
func (c *jsonCodec) Write(conn io.Writer, m *codec.Message, b interface{}) error {
|
2020-11-27 17:43:13 +03:00
|
|
|
switch m := b.(type) {
|
2020-11-29 21:08:49 +03:00
|
|
|
case nil:
|
|
|
|
return nil
|
2020-11-27 17:43:13 +03:00
|
|
|
case *codec.Frame:
|
|
|
|
_, err := conn.Write(m.Data)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewEncoder(conn).Encode(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
|
|
|
}
|