Skip to content
New issue

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

encoding/json: marshaling a zero value json.Marshaler interface struct field panics #16042

Closed
advincze opened this issue Jun 12, 2016 · 4 comments

Comments

Projects
None yet
6 participants
@advincze
Copy link

commented Jun 12, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    1.6
  2. What operating system and processor architecture are you using (go env)?
    darwin/amd6
  3. What did you do?
    https://play.golang.org/p/MsVP9u-We5
  4. What did you expect to see?
    {"M":null}
    https://golang.org/pkg/encoding/json/#Marshal says "A nil interface value encodes as the null JSON object."
  5. What did you see instead?
    panic: interface conversion: interface is nil, not json.Marshaler
@as

This comment has been minimized.

Copy link
Contributor

commented Jun 12, 2016

This issue is the result of this discussion on golang-nuts: https://groups.google.com/forum/#!topic/golang-nuts/rSfgMzknNIY

@ianlancetaylor ianlancetaylor changed the title marshaling a zero value json.Marhaler interface struct field panics encoding/json: marshaling a zero value json.Marshaler interface struct field panics Jun 12, 2016

@ianlancetaylor ianlancetaylor added this to the Go1.8 milestone Jun 12, 2016

@adg

This comment has been minimized.

Copy link
Contributor

commented Jun 14, 2016

It's a bad error message. The panic should be about attempting to call a nil MarshalJSON method, and not about the interface conversion.

@adg

This comment has been minimized.

Copy link
Contributor

commented Jun 14, 2016

Also if the code is bailing early on seeing a nil json.Marshaler value, then it shouldn't; it's possible for a json.Marshaler whose concrete type is nil to correctly implement the method.

@quentinmit quentinmit added the NeedsFix label Oct 6, 2016

@gopherbot

This comment has been minimized.

Copy link

commented Oct 25, 2016

CL https://golang.org/cl/31932 mentions this issue.

@gopherbot gopherbot closed this in add721e Nov 11, 2016

@golang golang locked and limited conversation to collaborators Nov 11, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.