外部程序包的自定义JSON封送处理

问题描述:

如何从外部包向结构添加自定义JSON封送处理?我无法将MarshalJSONUnmarshalYAML添加到这些结构中.

How can I add custom JSON marshaling to structs from external packages? I am unable to add the MarshalJSON and UnmarshalYAML to these structs.

您可以将该结构添加到您定义的结构中.然后将功能MarshalJSONUnmarshalYAML添加到您的结构中:

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) {
    ...
}