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
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@advincze
Copy link

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
Copy link
Contributor

as 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
Copy link
Contributor

adg 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
Copy link
Contributor

adg 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 The path to resolution is known, but the work has not been done. label Oct 6, 2016
@gopherbot
Copy link

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

@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.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants