You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've found that, when encoding a struct into JSON, if there is a space after the colon character in the tag for the field in the struct in question, then the field will not be renamed. Below is some very simple code demonstrating what I mean.
As you can see, in the above code I've made the apparent "mistake" of adding a space in my JSON tags.
If I run this code, the output I get the following output.
{"Name":"Alice","Age":29}
Now if I change the struct definition to the following
and then recompile and run the code, I get the expected output
{"name":"Alice","age":29}
What's interesting is that this isn't a problem when decoding JSON. Reintroducing spaces after the colon character, and switching to decoding from bytes, you'll see that the struct is populated properly even though I have switched the order of the fields in the JSON. I take this to mean that the decoder understands the JSON tags even with the space when decoding.
I should note that I've also experimented with using a Decoder and Encoder instead of Unmarshal and Marshal. They suffer from the same problem (and I assume share a similar implementation to their respective counterparts).
I tested this running Go 1.5.3 on the latest OS X. The output of go version is
go version go1.5.3 darwin/amd64
It seems to me that the encoding/json's decoding and encoding functionality should behave the same with regards to how struct tags are treated. I should be able to have spaces after ":" in the tag and still have struct fields encoded such that the right keys are used, especially since decoding isn't affected by this.
The text was updated successfully, but these errors were encountered:
@ianlancetaylor Just because it's "intended" to work that way doesn't mean it's good.
This behavior is misleading and confusing. There is no indication, such as a compile-time warning, about the behavior, and it's quite confusing. It necessitates an understanding of a very particular and frankly unusual piece of trivia about the Go compiler, and I think it's not in the language's interest to leave something so vague.
I'm not sure I understand why, in a language whose formatting tools encourage some superfluous whitespace between things like struct field names and their types, a little extra checking can't be done to avoid subtle cases like this.
Can this not be a discussion that isn't just shut down because of what your sacred spec says?
Yes, this can be a discussion, but the discussion should not take place on the bug tracker. It's not a bug: the code is working as documented and the documentation seems reasonably clear. For discussions, please see https://golang.org/wiki/Questions . Thanks.
I've found that, when encoding a struct into JSON, if there is a space after the colon character in the tag for the field in the struct in question, then the field will not be renamed. Below is some very simple code demonstrating what I mean.
As you can see, in the above code I've made the apparent "mistake" of adding a space in my JSON tags.
If I run this code, the output I get the following output.
Now if I change the
struct
definition to the followingand then recompile and run the code, I get the expected output
What's interesting is that this isn't a problem when decoding JSON. Reintroducing spaces after the colon character, and switching to decoding from bytes, you'll see that the struct is populated properly even though I have switched the order of the fields in the JSON. I take this to mean that the decoder understands the JSON tags even with the space when decoding.
The output I get is, as expected,
I should note that I've also experimented with using a Decoder and Encoder instead of Unmarshal and Marshal. They suffer from the same problem (and I assume share a similar implementation to their respective counterparts).
I tested this running Go 1.5.3 on the latest OS X. The output of
go version
isIt seems to me that the encoding/json's decoding and encoding functionality should behave the same with regards to how struct tags are treated. I should be able to have spaces after ":" in the tag and still have struct fields encoded such that the right keys are used, especially since decoding isn't affected by this.
The text was updated successfully, but these errors were encountered: