JSON field tag options are sensitive to whitespace
exhaze opened this issue · 3 comments
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.
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.
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.
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.