-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: make "json" tag user-settable #7337
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
Labels
Comments
Struct tags are nice but I think this overuses them a bit. Custom JSON marshalling can be done by implementing the Marshaler interface already. Why not type User struct { /* whatever */ } type APIUser User func (u APIUser) MarshalJSON() ([]byte, error) { // Manually marshal only the fields you want to expose. } u := User{} json.Marshal(u) // for all fields json.Marshal(APIUser(u)) // just some fields |
What about adding a field "Tag string" to Decoder such that I can choose which tag gets used for marshalling? The current behavior uses the magic string "json", and it would be nice to be able to change this. This would give the nice behavior without promoting use of multiple tags by having it be on an exported function. The problem with the prior suggestion of using MarshalJSON() is that *every* type that embeds a type that implements it would have to implement it as well: http://play.golang.org/p/B5W8uMs3bA I understand the opposition to polluting tags, but perhaps there's a way to go mid-way? If this idea isn't acceptable either, perhaps an interactive marshal would work (kind of like flag's Visit)? This would allow doing something like MarshalJSON, but on a per-field basis. |
Comment 4 by google@0xc0dedbad.com: > What about adding a field "Tag string" to Decoder such that I can choose which tag gets used for marshalling? The current behavior uses the magic string "json", and it would be nice to be able to change this. This would give the nice behavior without promoting use of multiple tags by having it be on an exported function. I'd really love to be able to pass a tag string to the encoder/decoder to make it easy to input/output different representations of a struct. It keeps all representations in a single authoritative location, and allows use of a consistent, easily understood method for doing so, for a pretty trivial change. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
by beatgammit:
The text was updated successfully, but these errors were encountered: