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

MarshalJSON outputs too many newlines #25

Open
bojanz opened this issue Jun 15, 2021 · 0 comments
Open

MarshalJSON outputs too many newlines #25

bojanz opened this issue Jun 15, 2021 · 0 comments

Comments

@bojanz
Copy link

bojanz commented Jun 15, 2021

Current implementation:

		// add key
		if err := encoder.Encode(k); err != nil {
			return nil, err
		}
		buf.WriteByte(':')
		// add value
		if err := encoder.Encode(o.values[k]); err != nil {
			return nil, err
		}

encoder.Encode will always add a newline:

	// Terminate each value with a newline.
	// This makes the output look a little nicer
	// when debugging, and some kind of space
	// is required if the encoded value was a number,
	// so that the reader knows there aren't more
	// digits coming.
	e.WriteByte('\n')

So we always get a newline after the key and after the value:

{"id"
:"123456"
,"status"
:"active"

This could be avoided by using json.Marshal(), but json.Marshal doesn't support turning off HTML encoding. So in order to preserve BC, we need to have a helper that will strip the last byte from the encoded output.

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

No branches or pull requests

1 participant