Skip to content

encoding/json: Unmarshal refuses to deserialize into custom interface var, even though the var points to a concrete object. #15535

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

Closed
HouzuoGuo opened this issue May 4, 2016 · 4 comments

Comments

@HouzuoGuo
Copy link

HouzuoGuo commented May 4, 2016

  1. What version of Go are you using (go version)?
    go1.6.1 linux/amd64
  2. What operating system and processor architecture are you using (go env)?
    Linux, amd64
  3. What did you do?
    json.Unmarshal refuses to deserialise JSON into a custom interface var, even though the var (interface) points to a concrete object.
  4. What did you expect to see?
    json.Unmarshal should be able to inspect the concrete object behind custom interface to deserialise JSON, instead of throwing message "cannot unmarshal object into Go value of type "

Full example, including a workaround:
https://play.golang.org/p/arj2sWQQnC

@cespare
Copy link
Contributor

cespare commented May 4, 2016

@HouzuoGuo you have a pointer to an interface value containing a Lemon, but what you actually want is an interface value containing a pointer to a Lemon.

https://play.golang.org/p/FyZp9CuQVo

@HouzuoGuo
Copy link
Author

HouzuoGuo commented May 4, 2016

@cespare thanks and I understood that my case is unconventional, but still I wish json.Unmarshal could figure it out without having to use reflect.

@extemporalgenome
Copy link
Contributor

@HouzuoGuo pointer to interface of value is not equivalent to interface of
pointer, and only interface of pointer is sufficient. Interfaces do not
extend addressability to their contents like other kinds of values do, so
even though, with the pointer, the interface value itself is now
addressable, the boxed value is still a semantic copy of what you actually
want to modify.

On Wed, May 4, 2016, 2:22 AM Howard notifications@github.com wrote:

@cespare https://github.com/cespare thanks and I understood that my
case is unconventional, but still I wish json.Unmarshal could figure it out
without having to use reflex.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#15535 (comment)

@bradfitz bradfitz changed the title encoding/json: json.Unmarshal refuses to deserialise into custom interface var, even though the var points to a concrete object. encoding/json: Unmarshal refuses to deserialize into custom interface var, even though the var points to a concrete object. May 4, 2016
@bradfitz
Copy link
Contributor

bradfitz commented May 4, 2016

Sorry, the json package is pretty frozen. Regardless of whether it might be a good idea to make this work, at this point we're not going to make changes.

@bradfitz bradfitz closed this as completed May 4, 2016
@golang golang locked and limited conversation to collaborators May 4, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants