是否可以忽略MarshalJSON
结构的自定义实现,而只使用标准编组函数?
该结构很复杂,并且有很多嵌套结构,所有这些都使用 custom MarshalJSON
,我想忽略它们。
我觉得应该是微不足道的。你有想法吗?
一些细节
创建新类型的明显解决方案效果不佳,因为嵌套结构仍然使用它们MarshalJSON
的 s.
以下是代码示例:
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)
}
(来源:https ://github.com/Azure/azure-sdk-for-go/blob/v62.0.0/services/resources/mgmt/2020-10-01/resources/models.go#L366 )
并且有很多属性(例如Name
等),我希望在我的 JSON 中看到这些属性(对于Properties
其他嵌套结构也是如此)。
此代码的 Python 实现提供了该数据,我的软件使用它,并且我(将代码移植到 Go)也希望能够从我的 Go 程序中导出这些数据。