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. Expected type string -> found int. #26928

Closed
eloyucu opened this issue Aug 10, 2018 · 1 comment
Closed

encoding/json: Unmarshal. Expected type string -> found int. #26928

eloyucu opened this issue Aug 10, 2018 · 1 comment

Comments

@eloyucu
Copy link

eloyucu commented Aug 10, 2018

Hi everyone.

This issue is about the json package, concretely the Unmarshal function.

My version of go: 1.10.2 windows/amd and the go env is not relevant because it happens too on play.golang.org (see the link with the example here: https://play.golang.org/p/NGoR7Vs-VxX ).

The problem I am finding is that I am receiving a json that includes an array like this:

[
  {
    "key": "accept-encoding",
    "value": "gzip, deflate"
  },
  {
    "key": "content-length",
    "value": 1377
  }
]

My question is... why should value field be a interface type?? I mean... every number can be converted to string without any problem... so, why can´t I use string as type and the json decoder makes responsible to realize the conversion (if it is necessary)??

I have been diving a little on the code of the decode file, and... in this line:

if v.Kind() == reflect.String && v.Type() == numberType {

removing the second condition of the if (&& v.Type() == numberType) works for me. I guess it is a security statement or maybe something important that I don´t understand. But... when does v.Type() will be different than v.Kind()?? or... when does the v.Kind() will be a string and the v.Type() a json.Number??

Thanks in advance.

PS: Finally I found this solution using UnmarshalJSON function: https://play.golang.org/p/egqc6KJK1-t but I would want to know the reason of the non-autoconversion.

@meirf meirf changed the title Json Unmarshal. Expected type string -> found int. encoding/json: Unmarshal. Expected type string -> found int. Aug 11, 2018
@agnivade
Copy link
Contributor

Hi @eloyucu ,

This appears to be a question rather than a Go bug. We do not use the issue tracker for general discussions or questions like these. The Github bug tracker is only used for tracking bugs and proposals going through the Proposal Process.

Please feel free to reach out to the forums in the Questions wiki page; it has a list of good places for asking questions.

Closing since this is not a bug.

@golang golang locked and limited conversation to collaborators Aug 11, 2019
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

3 participants