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: Invalid json is parsed ok #21227

Closed
cassiobotaro opened this issue Jul 31, 2017 · 2 comments

Comments

Projects
None yet
4 participants
@cassiobotaro
Copy link

commented Jul 31, 2017

Please answer these questions before submitting your issue. Thanks!

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

1.8.3

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

GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/home/cassiobotaro/go" GORACE="" GOROOT="/usr/lib/go" GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build528676691=/tmp/go-build -gno-record-gcc-switches" CXX="g++" CGO_ENABLED="1" PKG_CONFIG="pkg-config" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2"

What did you do?

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

What did you expect to see?

An error because json is not valid.

What did you see instead?

No errors.

@dcheney-atlassian

This comment has been minimized.

Copy link

commented Jul 31, 2017

IMO this is working as intended. Your example contains two fragments of json {} which is valid, and } which is not. The json package correctly detects the malformed second json fragment.

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

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jul 31, 2017

What @dcheney-atlassian said. The docs do say:

A Decoder reads and decodes JSON values from an input stream.
...
Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.

If you only want to read one element, use Unmarshal or use https://golang.org/pkg/encoding/json/#Decoder.Token to see if EOF is next.

@bradfitz bradfitz closed this Jul 31, 2017

@mikioh mikioh changed the title Invalid json is parsed ok encoding/json: Invalid json is parsed ok Aug 2, 2017

@golang golang locked and limited conversation to collaborators Aug 2, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.