encoding/json: Marshal silently ignores custom implementations on pass by value #30351
Labels
FrozenDueToAge
WaitingForInfo
Issue is not actionable because of missing required information, which needs to be provided.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Carefully implemented
json.Marshaler
andjson.Unmarshaler
interfaces, and passed my struct directly tojson.Marshal()
.What did you expect to see?
The resulting JSON byte array uses my custom
json.Marshaler
interface method.What did you see instead?
The resulting JSON byte array ignores my custom
json.Marshaler
interface method, instead presenting the keys and values of the plain Go struct declaration. This causes downstream breakages, as my custom JSON marshaling is needed to offset some issues with the data I happen to be handling.Workaround
As a workaround, I am making sure that I pass a reference (
&
) of my object to thejson.Marshal()
method. This is quite dangerous, as it is a really easy mistake to make, across a large codebase. And currently no way to validate this with the Go type system on the existing API. With other Go API calls, I at least get an annoying compile about needing a receiver pointer.In the future, can we improve the
json
API to prevent this from happening?The text was updated successfully, but these errors were encountered: