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: accept float starting with "." (".85") while unmarshaling json with ,string tag #32117

Open
ashwin1dd opened this issue May 17, 2019 · 4 comments

Comments

@ashwin1dd
Copy link

commented May 17, 2019

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

❯ go version
go version go1.12.5 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ashwin/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/ashwin/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.5/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nl/_f7rxwqs2y730r18nk0shs240000gn/T/go-build739953451=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Tried unmarshaling json that contained float string starting with "."
The playground link below contains 3 examples

  1. json unmarshal with ".85" -- fails
  2. json unmarshal with "0.85" -- succeeds
  3. strconv.ParseFloat ".85" -- succeeds

https://play.golang.org/p/q4OFvV6Asxt

What did you expect to see?

I expect ".85" to be successfully converted to a float using unmarshal
".85" -> 0.85

What did you see instead?

this error: json: invalid use of ,string struct tag, trying to unmarshal ".85" into float64

@josharian

This comment has been minimized.

Copy link
Contributor

commented May 17, 2019

That’s not valid json. See the number flow chart at http://json.org/

@bradfitz

This comment has been minimized.

Copy link
Member

commented May 18, 2019

But I think this is specifically about the ,string modifier in our JSON package. We could make it work if we wanted, without violating the JSON grammar.

@bradfitz bradfitz changed the title Accept float starting with "." (".85") while unmarshaling json with ,string tag encoding/json: accept float starting with "." (".85") while unmarshaling json with ,string tag May 18, 2019
@bradfitz bradfitz added this to the Go1.14 milestone May 18, 2019
@jamdagni86

This comment has been minimized.

Copy link
Contributor

commented May 21, 2019

@bradfitz / @josharian does this need to be fixed?

@josharian

This comment has been minimized.

Copy link
Contributor

commented May 21, 2019

It is waiting for a decision to be made. That might take a while. There definitely won’t be any code changes for it in the next month and a half or so.

Also, cc @mvdan

@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.