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: support decoding to []int from []string by applying tag ,string #15624

localvar opened this issue May 10, 2016 · 8 comments


Copy link

@localvar localvar commented May 10, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?

  2. What operating system and processor architecture are you using (go env)?
    set GOARCH=amd64
    set GOBIN=
    set GOEXE=.exe
    set GOHOSTARCH=amd64
    set GOHOSTOS=windows
    set GOOS=windows
    set GOPATH=D:\litead\oxygen
    set GORACE=
    set GOROOT=C:\go
    set GOTOOLDIR=C:\go\pkg\tool\windows_amd64
    set CC=gcc
    set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
    set CXX=g++
    set CGO_ENABLED=1

  3. What did you do?
    If possible, provide a recipe for reproducing the error.
    A complete runnable program is good.
    A link on is best.

  4. What did you expect to see?
    decode the int64 slice successfully

  5. What did you see instead?
    an error:

    json: cannot unmarshal string into Go value of type int64

according to the documentation, the behavior is correct, but this makes it hard to decode an int64 slice (although there are workarounds). It will be good if 'string' tag could also be applied to slices.

@bradfitz bradfitz added this to the Unplanned milestone May 10, 2016
Copy link

@jheiselman jheiselman commented Aug 12, 2016

I believe that this may be affecting the ability to parse JSON responses from the Slack messaging platform. Recently, my go-based Slack bot has been failing to parse the initial response after authenticating, but nothing appears to have changed in the server response and the response does successfully pass jslint.

@odeke-em odeke-em changed the title encoding/json: unable to decode int64 slice encoding/json: support decoding to []int from []string by applying tag ,string Aug 19, 2016
@odeke-em odeke-em self-assigned this Aug 20, 2016
Copy link

@odeke-em odeke-em commented Aug 20, 2016

Copy link

@gopherbot gopherbot commented Aug 20, 2016

CL mentions this issue.

Copy link

@localvar localvar commented Aug 22, 2016

@odeke-em , Thanks for the CL.
If I'm reading the code correct, tags like json:"i64" is used in the fix, this seems not consistent with other tags, for example when we want an int64, we use: json:",string"

Copy link

@odeke-em odeke-em commented Aug 22, 2016

@localvar thanks for taking a look at it, let's put comments on the CL there though.
But anyways, am using "i64" as the key and not as the auxilliary tag; it is so that the respective Go struct attribute will receive the unmarshaled data of json data key "i64", so

{"i64": ["12", "33", "999"]}

and Go code

type receiver struct {
   I64 []uint64 `json:"i64,string"`

the data in "i64" will be unmarshaled and assigned to I64.
It could have been

"keys" -> I64 []int `json:"keys,string"`

But let's move discussion to the CL itself to avoid noise here but also to ensure that we don't miss out on concerns that should be addressed in the CL.

@bradfitz bradfitz added this to the Go1.9Maybe milestone May 24, 2017
@bradfitz bradfitz removed this from the Unplanned milestone May 24, 2017
Copy link

@bradfitz bradfitz commented May 24, 2017

@rsc, can I get a JSON decision here?

@bradfitz bradfitz removed this from the Go1.9Maybe milestone Jul 20, 2017
@bradfitz bradfitz added this to the Go1.10 milestone Jul 20, 2017
@bradfitz bradfitz added this to the Go1.10 milestone Jul 20, 2017
@bradfitz bradfitz removed this from the Go1.9Maybe milestone Jul 20, 2017
@rsc rsc removed this from the Go1.10 milestone Nov 22, 2017
@rsc rsc added this to the Go1.11 milestone Nov 22, 2017
@ianlancetaylor ianlancetaylor removed this from the Go1.11 milestone Jun 29, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Jun 29, 2018
Copy link

@rskumar rskumar commented Jul 25, 2018

This is like fixing someone's wrong JSON by using some magic in tags. This shouldn't be language or core libs part.

Rather, if we can attach a field processor func, where we get raw bytes and json field tag and do our work explicitly and then return expected type value or error. The returned post-processed value will then be assigned to that field by JSON unmarshaller.

Copy link

@titpetric titpetric commented Oct 26, 2018

Hey, is there anything I could do to push this along? I have a few use cases where I need to deal with encoding a slice of uint64's, and am having issues inter-operating with javascript in the browser. I wouldn't like to resort to a typed uint64 and custom (un)marshallers to resolve this encoding issue, if there's a chance we can push this into go 1.12? I see there are a only a couple of code review comments open, and the concerns shouldn't be hard to resolve. Thoughts?

@rsc rsc removed this from the Go1.12 milestone Nov 14, 2018
@rsc rsc added this to the Go1.13 milestone Nov 14, 2018
darh added a commit to crusttech/crust-server that referenced this issue Apr 27, 2019
Due to golang's inability do decode uint64 slice from string slice, we're expecting
string input for members (for now): golang/go#15624
@andybons andybons removed this from the Go1.13 milestone Jul 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
10 participants