We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please answer these questions before submitting your issue. Thanks!
go version
go version go1.10 windows/amd64
Yes
go env
set GOARCH=amd64 set GOBIN= set GOEXE=.exe set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOOS=windows
Creating one type that is alias to []byte will not trigger the Marshal, but triggers the Unmarshal, like:
[]byte
Marshal
Unmarshal
type MyType []byte type StructJSON struct { Name MyType } func (d *MyType) MarshalJSON() ([]byte, error) { // << Never will be triggered } func (d *MyType) UnmarshalJSON(data []byte) (err error) { // << Working :) }
See the demonstration at https://play.golang.org/p/FHOMVOq1oDD
I expect that the same result of UnmarshalJSON. In the demonstration the message of Marshal is working should be displayed.
UnmarshalJSON
Marshal is working
The MarshalJSON() are not beginning triggered and don't have the same behavior of UnmarshalJSON.
MarshalJSON()
The text was updated successfully, but these errors were encountered:
Replacing *MyType to MyType will work.
*MyType
MyType
Sorry, something went wrong.
In your playground example, replace result, _ := json.Marshal(st) with result, _ := json.Marshal(&st)
result, _ := json.Marshal(st)
result, _ := json.Marshal(&st)
The Marshal method is defined on a pointer receiver. If you defined it as
func (d MyType) MarshalJSON() ([]byte, error) { ... }
Then result, _ := json.Marshal(st) would work, like you said.
No branches or pull requests
Please answer these questions before submitting your issue. Thanks!
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
)?What did you do?
Creating one type that is alias to
[]byte
will not trigger theMarshal
, but triggers theUnmarshal
, like:See the demonstration at https://play.golang.org/p/FHOMVOq1oDD
What did you expect to see?
I expect that the same result of
UnmarshalJSON
. In the demonstration the message ofMarshal is working
should be displayed.What did you see instead?
The
MarshalJSON()
are not beginning triggered and don't have the same behavior ofUnmarshalJSON
.The text was updated successfully, but these errors were encountered: