外部软件包的自定义golang JSON封送处理
问题描述:
How can I add custom JSON marshaling to structs from external packages? I am unable to add the MarshalJSON
and UnmarshalYAML
to these structs.
如何从外部包向结构添加自定义JSON封送处理? 我无法将 MarshalJSON code>和
UnmarshalYAML code>添加到这些结构中。 p>
div>
答
You could add that struct to one defined by you. Then add the functions MarshalJSON
and UnmarshalYAML
to your struct:
type YourStruct struct {
ImportedStruct
}
func (s *YourStruct) MarshalJSON() ([]byte, error) {
...
}