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: Marshal and Encode differ #47063

Closed
masseelch opened this issue Jul 6, 2021 · 2 comments
Closed

encoding/json: Marshal and Encode differ #47063

masseelch opened this issue Jul 6, 2021 · 2 comments

Comments

@masseelch
Copy link

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

$ go version
go version go1.16.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
O111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/_/.cache/go-build"
GOENV="/home/_/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/_/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/_/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/_/Code/go_pkgs/elk/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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3215158634=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Having an app serialize structs with json.Encode.Encode() to write the data to an io.Writer. Running tests with json.Marshal I noticed that the generated data differs: json.Encoder.Encode() adds a newline at the end while json.Marshal does not. Is this intended? Is this a bug?

Example

What did you expect to see?

They have the same output.

What did you see instead?

They do not have the same output.

@mdlayher
Copy link
Member

mdlayher commented Jul 6, 2021

https://golang.org/pkg/encoding/json/#Encoder

The Encoder is meant for writing a stream of JSON values and is documented to add a newline. Closing, I believe this is working as intended.

@mdlayher mdlayher closed this as completed Jul 6, 2021
@masseelch
Copy link
Author

This totally slipped my research. You are correct, I apologize.

@golang golang locked and limited conversation to collaborators Jul 6, 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