mirror of
https://github.com/go-micro/go-micro.git
synced 2026-05-06 19:21:46 +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>
33 lines
626 B
Go
33 lines
626 B
Go
package json
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
var protojsonMarshaler = protojson.MarshalOptions{
|
|
EmitUnpopulated: false,
|
|
}
|
|
|
|
type Marshaler struct{}
|
|
|
|
func (j Marshaler) Marshal(v interface{}) ([]byte, error) {
|
|
if pb, ok := v.(proto.Message); ok {
|
|
return protojsonMarshaler.Marshal(pb)
|
|
}
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func (j Marshaler) Unmarshal(d []byte, v interface{}) error {
|
|
if pb, ok := v.(proto.Message); ok {
|
|
return protojson.Unmarshal(d, pb)
|
|
}
|
|
return json.Unmarshal(d, v)
|
|
}
|
|
|
|
func (j Marshaler) String() string {
|
|
return "json"
|
|
}
|