@@ -32,6 +32,10 @@ type EncoderOptions struct {
3232 // EnumAsString when set will encode enums as strings instead of integers.
3333 // Caution: Enabling this option produce different sign bytes.
3434 EnumAsString bool
35+ // AminoNameAsTypeURL when set will use the amino name as the type URL in the JSON output.
36+ // It is useful when using the Amino JSON encoder for non Amino purposes,
37+ // such as JSON RPC.
38+ AminoNameAsTypeURL bool
3539 // TypeResolver is used to resolve protobuf message types by TypeURL when marshaling any packed messages.
3640 TypeResolver signing.TypeResolver
3741 // FileResolver is used to resolve protobuf file descriptors TypeURL when TypeResolver fails.
@@ -50,6 +54,7 @@ type Encoder struct {
5054 doNotSortFields bool
5155 indent string
5256 enumsAsString bool
57+ aminoNameAsTypeURL bool
5358}
5459
5560// NewEncoder returns a new Encoder capable of serializing protobuf messages to JSON using the Amino JSON encoding
@@ -80,11 +85,12 @@ func NewEncoder(options EncoderOptions) Encoder {
8085 "google.protobuf.Duration" : marshalDuration ,
8186 "google.protobuf.Any" : marshalAny ,
8287 },
83- fileResolver : options .FileResolver ,
84- typeResolver : options .TypeResolver ,
85- doNotSortFields : options .DoNotSortFields ,
86- indent : options .Indent ,
87- enumsAsString : options .EnumAsString ,
88+ fileResolver : options .FileResolver ,
89+ typeResolver : options .TypeResolver ,
90+ doNotSortFields : options .DoNotSortFields ,
91+ indent : options .Indent ,
92+ enumsAsString : options .EnumAsString ,
93+ aminoNameAsTypeURL : options .AminoNameAsTypeURL ,
8894 }
8995 return enc
9096}
@@ -187,9 +193,17 @@ func (enc Encoder) beginMarshal(msg protoreflect.Message, writer io.Writer, isAn
187193 )
188194
189195 if isAny {
190- name , named = getMessageAminoNameAny (msg ), true
196+ if enc .aminoNameAsTypeURL {
197+ name , named = getMessageTypeURL (msg ), true
198+ } else {
199+ name , named = getMessageAminoNameAny (msg ), true
200+ }
191201 } else {
192202 name , named = getMessageAminoName (msg )
203+ if enc .aminoNameAsTypeURL {
204+ // do not override named
205+ name = getMessageTypeURL (msg )
206+ }
193207 }
194208
195209 if named {
0 commit comments