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: encoding.UnmarshalText() produces unexpected values when json.Unmarshaling. #38046

Closed
rkolp opened this issue Mar 24, 2020 · 3 comments

Comments

@rkolp
Copy link

@rkolp rkolp commented Mar 24, 2020

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

$ go version
go version go1.14 darwin/amd64

Does this issue reproduce with the latest release?

Yes, however it seems v1.13.X is working properly.

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users//Library/Caches/go-build"
GOENV="/Users//Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users//develop/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users//develop/go/src/github.com//go.mod"
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/2w/0n_2xt5x51sbnz4__k1slqnw0000gn/T/go-build451039144=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I tried to implement encoding.TextUnmarshaler for unmarshaling JSON. However when escaped characters are added to the end of a JSON value the unmarshaled value becomes mangled and includes JSON formatting characters.

package main

import (
	"encoding/json"
	"fmt"
	"strings"
)

type RC string

func (r *RC) UnmarshalText(b []byte) error {
	*r = RC(fmt.Sprintf("RC_%s", strings.ToUpper(string(b))))
	return nil
}

type incident struct {
	Regions map[RC]Region `json:"regions"`
}

type Region struct {
	Facets []Facet `json:"facets"`
}

type Facet struct {
	SOE string `json:"sensitivity_override_explanation"`
}

func main() {
	r := `
	{
		"regions": {
			"US": {
				"facets": [{
				"sensitivity_override_explanation": "43423432423\\r"
				}]
			}
		}
	}
	`
	i := new(incident)

	if err := json.Unmarshal([]byte(r), &i); err != nil {
		fmt.Println(err)
		return
	}

	for k := range i.Regions {
		fmt.Printf("key: %q\n", k)
	}
}

What did you expect to see?

key: "RC_US"

What did you see instead?

key: "RC_US\": {\n\t\t\t"

@rkolp rkolp changed the title encoding.TextUnmarshal() produces unexpected values when json.Unmarshaling. encoding.UnmarshalText() produces unexpected values when json.Unmarshaling. Mar 24, 2020
@andybons andybons changed the title encoding.UnmarshalText() produces unexpected values when json.Unmarshaling. encoding/json: encoding.UnmarshalText() produces unexpected values when json.Unmarshaling. Mar 24, 2020
@andybons
Copy link
Member

@andybons andybons commented Mar 24, 2020

@andybons andybons added this to the Unplanned milestone Mar 24, 2020
@rkolp
Copy link
Author

@rkolp rkolp commented Mar 27, 2020

Looks like someone else ran into this issue as well: #38105

@mvdan
Copy link
Member

@mvdan mvdan commented Mar 27, 2020

Thanks @rkolp. It looks like @dsnet already did a bisect and a backport issue there, so let's consolidate both issues into that one. I realise this issue is older, so apologies for the mishap.

@mvdan mvdan closed this Mar 27, 2020
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.