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

Open
localvar opened this Issue May 10, 2016 · 8 comments

Comments

Projects
None yet
9 participants
@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)?
    1.6

  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 GO15VENDOREXPERIMENT=1
    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 play.golang.org is best.
    https://play.golang.org/p/A4CLM3P6Dw

  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

@jheiselman

This comment has been minimized.

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 from encoding/json: unable to decode int64 slice to 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

@odeke-em

This comment has been minimized.

Member

odeke-em commented Aug 20, 2016

@gopherbot

This comment has been minimized.

gopherbot commented Aug 20, 2016

CL https://golang.org/cl/27453 mentions this issue.

@localvar

This comment has been minimized.

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"

@odeke-em

This comment has been minimized.

Member

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.
Thanks.

@bradfitz bradfitz modified the milestones: Go1.9Maybe, Unplanned May 24, 2017

@bradfitz

This comment has been minimized.

Member

bradfitz commented May 24, 2017

@rsc, can I get a JSON decision here?

@bradfitz bradfitz modified the milestones: Go1.9Maybe, Go1.10 Jul 20, 2017

@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 22, 2017

@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Go1.12 Jun 29, 2018

@rskumar

This comment has been minimized.

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.

@titpetric

This comment has been minimized.

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 modified the milestones: Go1.12, Go1.13 Nov 14, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment