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: Unintuitive behavior when using custom UnmarshalText #38947

Closed
glooms opened this issue May 8, 2020 · 3 comments
Closed

encoding/json: Unintuitive behavior when using custom UnmarshalText #38947

glooms opened this issue May 8, 2020 · 3 comments
Milestone

Comments

@glooms
Copy link

@glooms glooms commented May 8, 2020

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

$ go version
go version go1.14.2 linux/amd64

Does this issue reproduce with the latest release?

Yes, it even seems like it was introduced somewhere between go1.13.9 and go1.14.2.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/qlik/.cache/go-build"
GOENV="/home/qlik/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/qlik/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/snap/go/5646"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/snap/go/5646/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
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-build785744259=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I was trying to Unmarshal a JSON body containing \n into a map[String]string where String is TextUnmarshaler:

type String string

func (s *String) UnmarshalText(text []byte) error {
	*s = String(text)
	return nil
}

JSON body:

{
        "Pizza": "Pizza is yummy.\nIt is very yummy.",
        "Apple": "Apples are also yummy."
}

An example can be found here: https://play.golang.org/p/V7qY2khOcgH

Note, my key issue is the difference in behavior between the two maps in the playground example.

What did you expect to see?

A normal map without escaped quotes. Which is the result of using a map[string]string instead of a map[String]string.

What did you see instead?

Escaped quotes in the map keys. As if the key wasn't properly tokenized.

@glooms glooms changed the title Unintuitive behavior from json.Unmarshal when using custom UnmarshalText encoding/json: Unintuitive behavior when using custom UnmarshalText May 8, 2020
@mvdan
Copy link
Member

@mvdan mvdan commented May 8, 2020

This sounds like a duplicate of #38105, which has a fix in master that will be backported for 1.14.3.

@dmitshur dmitshur added this to the Go1.15 milestone May 8, 2020
@mvdan
Copy link
Member

@mvdan mvdan commented May 9, 2020

I can confirm that the tests from https://play.golang.org/p/V7qY2khOcgH pass on master, so this is a duplicate of the issue above. Follow #38106 for the backport to 1.14.3.

@mvdan mvdan closed this May 9, 2020
@glooms
Copy link
Author

@glooms glooms commented May 10, 2020

Thanks @mvdan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.