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: Unmarshal into *interface{} that contains specific value #6640

Closed
gopherbot opened this issue Oct 22, 2013 · 4 comments
Closed

encoding/json: Unmarshal into *interface{} that contains specific value #6640

gopherbot opened this issue Oct 22, 2013 · 4 comments

Comments

@gopherbot
Copy link

@gopherbot gopherbot commented Oct 22, 2013

by Lytvynov.A.V:

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
http://play.golang.org/p/PQ7zcqcdN-
Pass pointer to an interface{} value that contains specific struct, instead of
interface{} value that contains pointer to specific struct.

What is the expected output?
main.B{Key:"", Val:""}
main.B{Key:"one", Val:"1"}

What do you see instead?
main.B{Key:"", Val:""}
map[string]interface {}{"Key":"one", "Val":"1"}

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
linux

Which version are you using?  (run 'go version')
go version devel +b4c37131e846 Mon Oct 21 19:01:24 2013 -0400 linux/amd64
also works on release version

Please provide any additional information below.
Obvious solution to the code above would be http://play.golang.org/p/ffp11TvZoe
But should it *only* work that way?
@adg

This comment has been minimized.

Copy link
Contributor

@adg adg commented Oct 22, 2013

Comment 1:

The first version of the unmarshal function is useless. You're just passing in a struct
value. Even if it "worked", the unmarshal function would populate a copy of that value
and you could never retrieve the contents of the JSON blob.
@gopherbot

This comment has been minimized.

Copy link
Author

@gopherbot gopherbot commented Oct 22, 2013

Comment 2 by Lytvynov.A.V:

Indeed, but that was just a simplified example demonstrating the behavior.
Here are similar examples:
http://play.golang.org/p/nhpFM4roxD
and
http://play.golang.org/p/PDwJF7Kcxj
@adg

This comment has been minimized.

Copy link
Contributor

@adg adg commented Oct 22, 2013

Comment 3:

The issue is that the concrete value of the interface{} value is just a value, and is
not addressable, and so the JSON package can't modify it.
Change the line
    a := &A{B: B{}}
to
    a := &A{B: &B{}}
and you'll see that it works.
Here is a reduced example that demonstrates the difference:
  http://play.golang.org/p/AyC8gSZh-d

Status changed to WorkingAsIntended.

@gopherbot

This comment has been minimized.

Copy link
Author

@gopherbot gopherbot commented Oct 22, 2013

Comment 4 by Lytvynov.A.V:

Ah, so it's about addressability.
That makes it clearer. Thanks!
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
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
2 participants
You can’t perform that action at this time.