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: marshalling interfaces #11951

Closed
ThePiachu opened this issue Jul 30, 2015 · 2 comments
Closed

encoding/json: marshalling interfaces #11951

ThePiachu opened this issue Jul 30, 2015 · 2 comments

Comments

@ThePiachu
Copy link

@ThePiachu ThePiachu commented Jul 30, 2015

I recently started implementing some interfaces for my structures and I was surprised to find that the default JSON encoder appears to try encoding the interfaces as if they were variables in structures. So for example if we have:

type Foo interface{
FooMe()
}

type Bar struct {
Foo
ImportantVariable string
}

The encoded JSON would look like:
{"Foo":null,"ImportantVariable":""}

Which is kind of silly, since Foo will never be anything other than null.

I think it would be useful for JSON to check if an imported variable is only an interface, not a struct or any other data structure, and ignore it completely if it is.

@bradfitz

This comment has been minimized.

Copy link
Contributor

@bradfitz bradfitz commented Jul 30, 2015

Here it's not null:
http://play.golang.org/p/M-G2BsyqvV

And here it's a string:
http://play.golang.org/p/XAIGQr_15b

I think this is working.

@bradfitz bradfitz closed this Jul 30, 2015
@bradfitz bradfitz changed the title JSON marshalling interfaces encoding/json: marshalling interfaces Jul 30, 2015
@ThePiachu

This comment has been minimized.

Copy link
Author

@ThePiachu ThePiachu commented Jul 30, 2015

Sorry, I meant empt interface. One way or the other, it's trying to denote there is a variable there when we only have an interface that doesn't hold any data itself.

@golang golang locked and limited conversation to collaborators Aug 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.