You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
Generally this is fine, as protobufs aren't value-copyable and are almost always used in a pointer context, but if it's not a value receiver, there are subtle places where the json.Marshaler function won't be called properly.
For example:
typeEmbedFoostruct {
FooTimeToLiveint`json:"ttl"`
}
funcmain() {
foo:=Foo{}
_, _=json.Marshal(foo) // FAIL, Does not call MarshalJSON_, _=json.Marshal(&foo) // OKfoo2:=EmbedFoo{}
_, _=json.Marshal(foo2) // FAIL, Does not call MarshalJSON_, _=json.Marshal(&foo2) // FAIL, Does not call MarshalJSON
}
Changing the generated MarshalJSON to have value receiver fixes it (due to the pointer-following logic that json.Marshal() performs):
The generated
MarshalJSON()
implementation currently has a pointer receiver:Generally this is fine, as protobufs aren't value-copyable and are almost always used in a pointer context, but if it's not a value receiver, there are subtle places where the
json.Marshaler
function won't be called properly.For example:
Changing the generated MarshalJSON to have value receiver fixes it (due to the pointer-following logic that
json.Marshal()
performs):This would also mirror its common uses within Go itself (including the example in the docs):
Note: This only applies to MarshalJSON. UnmarshalJSON should continue to have the pointer receiver.
The text was updated successfully, but these errors were encountered: