-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
proposal: Struct field tags for directional Marshal/Unmarshal #18662
Comments
Why?
|
@minux There have been several occasions where I have had to implement marshaller methods to filter out fields depending on where the data is going. For example, in my models in an MVC application I sometimes like certain attributes to not be Marshalled, say a foreign key for an embedded type, back to the client and only allow it to be Unmarshalled from a request to set and perform business logic on. As I said, I do this already with the MarshalJSON/UnmarshalJSON, though it seems redundant when there already exists a similar functionality for |
In such applications, it's probably better to create separate structs that
don't have unneeded fields.
|
Yeah, as @minux suggested, it's probably better to just use two structs. As of Go 1.8 this is now easier. See https://beta.golang.org/doc/go1.8#language I'm going to decline this proposal, because the json package is pretty locked down at this point and it seems like there's already a solution. Even if the solution might not be ideal for you, it's probably better than complicating the json package for everybody else. |
@bradfitz What's the reasoning behind the omit ´json:"-"´option as that seems essentially the same. I could just create a struct that doesn't contain that field, but its cleaner and less copying to add the tag. |
The current package provides the following json tag capabilities as specified in the docs:
A feature I propose is an additional set of tag parameters that allow for definition of the directional conditions to marshal/unmarshal.
The text was updated successfully, but these errors were encountered: