type Alpha struct {
Name string `json:"name"`
SkipWhenMarshal string `json:"skipWhenMarshal"`
}
func MarshalJSON(out interface{}){
json.Marshal(out)
}
Is it possible to ignore the SkipWhenMarshal field when I do json.Marshal but not when I do json.Unmarshal. It should work for any type who calls MarshalJSON
Field tag modifiers like "omitempty" and "-" apply to both marshaling and unmarshaling, so there's no automatic way.
You can implement a
MarshalJSONfor your type that ignores whatever fields you need. There's no need to implement a custom unmarshaler, because the default works for you.E.g. something like this:
You can also make it simpler by using an alias type:
Here
SkipWhenMarshalwill be output, but its value is zeroed out. The advantage in this approach is that ifAlphahas many fields, you don't have to repeat them.