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: Unmarshaling json.RawMessage leads to an unexpected result #46821

Closed
ebi-yade opened this issue Jun 18, 2021 · 2 comments
Closed

Comments

@ebi-yade
Copy link

ebi-yade commented Jun 18, 2021

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

$ go version
go version go1.16.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes, 1.16.5 is the latest version today!

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ebi-exp/Library/Caches/go-build"
GOENV="/Users/ebi-exp/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/ebi-exp/go/1.16.5/pkg/mod"
GONOPROXY="github.com/sikiapi"
GONOSUMDB="github.com/sikiapi"
GOOS="darwin"
GOPATH="/Users/ebi-exp/go/1.16.5"
GOPRIVATE="github.com/sikiapi"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/ebi-exp/.anyenv/envs/goenv/versions/1.16.5"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/ebi-exp/.anyenv/envs/goenv/versions/1.16.5/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ebi-exp/ghq/github.com/ebi-yade/go-json-sandbox/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 -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/s7/1j_fhlc91jj470cmn36hqs980000gn/T/go-build3900691258=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Here is a pull request for my sandbox repository. As the code and test actions, you would easily find the JSON Marshal / Unmarshal functions for json.RawMessage fields behave differently from ones for []byte. And the weirdest point is parsing a field of null as the json.RawMessage type results in not nil but the byte array equivalent to a string "null". However, in the cases for other (Go-standard) types, it interprets them as the zero value of the specified type (like 0 for numbers, "" for string, and even nil for []byte).

What did you expect to see?

I want the json.RawMessage to behave as possible as similar to []byte because it's the type alias, but if this is hard, it's enough that the null value is parsed into nil of json.RawMessage, as above.

What did you see instead?

I'm looking for the implementation code of UnmarshalJSON and MarshalJSON for []byte type and want to see the difference from the ones for json.RawMessage, but for now, I couldn't find them. And at least, it's definitely clear this issue can be reproduced on Go Playground: https://play.golang.org/p/YTHmX5FOg_x

@seankhliao
Copy link
Member

json.RawMessage is documented to return the unparsed json and not any other representation of it

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@ebi-yade
Copy link
Author

Hello Sean,

I'm sorry I overlooked that parsing JSON attributes of null returns the different bytes from parsing ones of "null" (Demo). So there is no inconsistency in the system thanks to referential transparency (even though I feel weird that the Unmarshal result doesn't end up a zero value).

Somehow, It was thoughtless of me to marking such a non-critical topic as a GitHub Issue and took your time 🙏

@golang golang locked and limited conversation to collaborators Jun 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants