如何定制json序列化
Marshal traverses the value v recursively.
If an encountered value implements the
Marshalerinterface and is not a nil pointer, Marshal calls itsMarshalJSONmethod to produce JSON.If no MarshalJSON method is present but the value implements
encoding.TextMarshalerinstead, Marshal calls itsMarshalTextmethod and encodes the result as a JSON string
也就是说,对于自定义类型,要支持json序列化,要实现以下接口:
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
type TextMarshaler interface {
MarshalText() (text []byte, err error)
}Last updated