外部程序包的自定义JSON封送处理 [英] Custom JSON marshaling for external packages

查看:42
本文介绍了外部程序包的自定义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) {
    ...
}

这篇关于外部程序包的自定义JSON封送处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆