Is it possible to ignore a custom MarshalJSON implementation of a struct,
and use just standard marshaling function instead?
The struct is complex, and has a lot of nested structs, all of which are
using custom MarshalJSON, and I would like to ignore them all.
I feel that it should be trivial. Do you have an idea?
Some details
An obvious solution with a new type creation does not work well, because the nested structs still use their MarshalJSONs.
Here is an example of the code:
func (de DeploymentExtended) MarshalJSON() ([]byte, error) {
objectMap := make(map[string]interface{})
if de.Location != nil {
objectMap["location"] = de.Location
}
if de.Properties != nil {
objectMap["properties"] = de.Properties
}
if de.Tags != nil {
objectMap["tags"] = de.Tags
}
return json.Marshal(objectMap)
}
And there are a lot of properties (like Name, etc), which I would like to see in my JSON (the same for Properties and other nested structs).
The Python implementation of this code provides that data, my software use it, and I (porting the code to Go) would like to be able to export these data from my Go program too.
MarshalJSONdefined on them to begin with.MarshalJSONlogic.MarshalJSON()because they implement how they should be marshaled into JSON. If this isn't what you want, then either the current implementations ofMarshalJSON()should be changed, or you shouldn't use the values of these types to begin with. There are no multiple variants of JSON marshaling. If you need different logic, you need different types (with different logic implemented).