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

Comments

Projects
None yet
2 participants
@eloyucu
Copy link

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

This comment has been minimized.

Copy link
Member

commented Aug 11, 2018

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.

@agnivade agnivade closed this Aug 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.