mirror of
https://github.com/go-micro/go-micro.git
synced 2026-06-09 19:16:58 +02:00
a100a47340
* Initial plan * Update JSON codec to use modern protojson for proper Any type support Co-authored-by: asim <17530+asim@users.noreply.github.com> * Add comprehensive tests for google.protobuf.Any JSON marshaling Co-authored-by: asim <17530+asim@users.noreply.github.com> * Revert codec/proto to old protobuf package for backward compatibility Co-authored-by: asim <17530+asim@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: asim <17530+asim@users.noreply.github.com>
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
// Package json provides a json codec
|
|
package json
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
|
|
"go-micro.dev/v5/codec"
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type Codec struct {
|
|
Conn io.ReadWriteCloser
|
|
Encoder *json.Encoder
|
|
Decoder *json.Decoder
|
|
}
|
|
|
|
func (c *Codec) ReadHeader(m *codec.Message, t codec.MessageType) error {
|
|
return nil
|
|
}
|
|
|
|
func (c *Codec) ReadBody(b interface{}) error {
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
if pb, ok := b.(proto.Message); ok {
|
|
// Read all JSON data from decoder
|
|
var raw json.RawMessage
|
|
if err := c.Decoder.Decode(&raw); err != nil {
|
|
return err
|
|
}
|
|
return protojson.Unmarshal(raw, pb)
|
|
}
|
|
return c.Decoder.Decode(b)
|
|
}
|
|
|
|
func (c *Codec) Write(m *codec.Message, b interface{}) error {
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
if pb, ok := b.(proto.Message); ok {
|
|
data, err := protojson.Marshal(pb)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Write the marshaled data to the encoder
|
|
var raw json.RawMessage = data
|
|
return c.Encoder.Encode(raw)
|
|
}
|
|
return c.Encoder.Encode(b)
|
|
}
|
|
|
|
func (c *Codec) Close() error {
|
|
return c.Conn.Close()
|
|
}
|
|
|
|
func (c *Codec) String() string {
|
|
return "json"
|
|
}
|
|
|
|
func NewCodec(c io.ReadWriteCloser) codec.Codec {
|
|
return &Codec{
|
|
Conn: c,
|
|
Decoder: json.NewDecoder(c),
|
|
Encoder: json.NewEncoder(c),
|
|
}
|
|
}
|