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

Open
localvar opened this issue May 10, 2016 · 11 comments
Open
Labels
FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@localvar
Copy link

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
Copy link

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
@odeke-em
Copy link
Member

I've mailed CL https://go-review.googlesource.com/27453.

@gopherbot
Copy link

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

@localvar
Copy link
Author

@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
Copy link
Member

@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 added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label May 24, 2017
@bradfitz bradfitz modified the milestones: Go1.9Maybe, Unplanned May 24, 2017
@bradfitz
Copy link
Contributor

@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
Copy link

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
Copy link

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
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
@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
@CarlCao17
Copy link

CarlCao17 commented Mar 28, 2024

Could the issue be pushed along the way?
I think I have encountered many times for communication with web, most of them are dealing with numeric IDs. JSON could not represent large integer numbers like int64 in Go, so usually we transfer id string to server and need to convert it to int64.

For single id, the ",string" go tag is very nice. However for batch ids request, we need to do a lot more, though we have some ways to solve it, such as implementing the customer type UnmarshalJSON method for []int64 or Int64OrStr. But we need to do these one time by one time for thoses ID requests and for Int64OrStr we need to convert it to Int64 when using Int64OrStr.
If the behavior could be in encoding/json package, I think it could be better, in my opinion.

@ianlancetaylor
Copy link
Contributor

At this point we expect to move to encoding/json/v2 (63397). CC @dsnet @mvdan

@dsnet
Copy link
Member

dsnet commented Mar 29, 2024

The v2 experiment currently recursively applies the meaning of string to elements of a Go slice or map, which would accomplish what this is asking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests