Jde to tam naohejbat se starou implementací.
```
// foo.proto
message Foo {
RawJSONMessage bar = 1;
}
message RawJSONMessage {
string value = 1;
}
```
```
// foo.go
import "github.com/golang/protobuf/jsonpb"
// MarshalJSONPB overrides how the Bar, protobuf generated struct, is being serialized to JSON.
func (b *Bar) MarshalJSONPB(*jsonpb.Marshaler) ([]byte, error) {
return []byte(b.Val), nil
}
```
```
// foo_test.go
import (
"testing"
"github.com/golang/protobuf/jsonpb"
)
func TestFoo(t *testing.T) {
f := &Foo{
Bar: &Bar{
Val: `{"foo":"bar"}`,
},
}
s, err := (&jsonpb.Marshaler{}).MarshalToString(f)
if err != nil {
t.Fatal(err)
}
t.Log(s)
}
```
Každopádně ta opravdová odpověď leží zde:
https://pkg.go.dev/github.com/golang/protobuf/jsonpb#JSONPBMarshaler