-
Notifications
You must be signed in to change notification settings - Fork 18.9k
encoding/json: wrong encoding for json.Number field with string option (struct tag) #34268
Copy link
Copy link
Closed
Closed
Copy link
Labels
FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.
Milestone
Description
If the string option is set on a struct field of type json.Number, the value gets encoded without quotes. If this encoded value is then unmarshaled back, it fails, because the expected quotes are missing.
What version of Go are you using (go version)?
$ go version go version go1.12.5 linux/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="/home/lubr/.cache/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/home/lubr/go" GOPROXY="" GORACE="" GOROOT="/home/lubr/.gvm/versions/go1.12.5.linux.amd64" GOTMPDIR="" GOTOOLDIR="/home/lubr/.gvm/versions/go1.12.5.linux.amd64/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" 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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build368255501=/tmp/go-build -gno-record-gcc-switches"
What did you do?
package main
import (
"encoding/json"
"fmt"
"log"
)
type stringTest struct {
JSONNumberStringOption json.Number `json:",string"`
}
func main() {
b, err := json.Marshal(stringTest{
JSONNumberStringOption: "2",
})
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
var res stringTest
err = json.Unmarshal(b, &res)
if err != nil {
log.Fatal(err)
}
}https://play.golang.org/p/iX9v1XWpsLG
What did you expect to see?
{"JSONNumberStringOption":"2"}
What did you see instead?
{"JSONNumberStringOption":2}
2019/09/12 21:33:02 json: invalid use of ,string struct tag, trying to unmarshal unquoted value into json.Number
exit status 1
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.