我搜索了一些类似的帖子,但是 Go JSON unmarshalling 是一个热门话题,在所有其他帖子中我看不到任何专门针对我的问题的内容。
有没有办法为现有类型添加/注册 JSON 解组逻辑——由外部库定义的类型?
例子:
import (
"go.mongodb.org/mongo-driver/bson/primitive"
)
type SomeDBModel struct {
Created primitive.DateTime
}
# NOTE: primitive.DateTime is an int64 and has implemented MarshalJSON,
# but not UnmarshalJSON.
# It marshals into an RFC 3339 datetime string; I'd like to be able to
# also unmarshal from RFC 3339 strings.
有什么方法可以将primitive.DateTime
对象的解组函数注册到 Go 的默认 JSON 解组器?我宁愿不嵌入primitive.DateTime
到包装结构中。