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

JSON field tag options are sensitive to whitespace #18531

Closed
exhaze opened this issue Jan 6, 2017 · 3 comments
Closed

JSON field tag options are sensitive to whitespace #18531

exhaze opened this issue Jan 6, 2017 · 3 comments

Comments

@exhaze
Copy link

exhaze commented Jan 6, 2017

Please answer these questions before submitting your issue. Thanks!

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

1.7.4

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

darwin/amd64

What did you do?

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

What did you expect to see?

type A struct { B *int `json:"b, omitempty"` } should serialize to {}

What did you see instead?

type A struct { B *int `json:"b, omitempty"` } serializes to {"b":null}

Happy to submit a patch to address this, just wanted to know if there wasn't a specific unstated reason why this is the way it is.

@bradfitz
Copy link
Contributor

bradfitz commented Jan 6, 2017

This is by design. We have never supported whitespace there, and don't intend to.

As the docs say:

https://golang.org/pkg/reflect/#StructTag

By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.

Note it says nothing about optional whitespace.

@bradfitz bradfitz closed this as completed Jan 6, 2017
@exhaze
Copy link
Author

exhaze commented Jan 9, 2017

This doesn't seem ideal and violates the principle of least surprise. Is there a reason you feel strongly about not supporting whitespace there?

If you feel strongly there should not be spaces in tag string values, a space in there should be a syntax error, or at the very least a linter error. Again, happy to write a patch, just wanted to understand your reasoning better first.

@bradfitz
Copy link
Contributor

bradfitz commented Jan 9, 2017

Go 1.8 includes new vet checks for this: https://beta.golang.org/doc/go1.8#tool_vet

If you find they're insufficient, please file a bug.

@golang golang locked and limited conversation to collaborators Jan 9, 2018
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