Documentation
¶
Index ¶
- Variables
- func FormMarshal(v interface{}) ([]byte, error)
- func FormUnmarshal(data []byte, v interface{}) error
- func JsonMarshal(v interface{}) ([]byte, error)
- func JsonUnmarshal(data []byte, v interface{}) error
- func ProtoMarshal(v interface{}) ([]byte, error)
- func ProtoUnmarshal(data []byte, v interface{}) error
- func XmlMarshal(v interface{}) ([]byte, error)
- func XmlUnmarshal(data []byte, v interface{}) error
- func YamlMarshal(v interface{}) ([]byte, error)
- func YamlUnmarshal(data []byte, v interface{}) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // MarshalOptions is a configurable JSON format marshaller. MarshalOptions = protojson.MarshalOptions{ EmitUnpopulated: true, } // UnmarshalOptions is a configurable JSON format parser. UnmarshalOptions = protojson.UnmarshalOptions{ DiscardUnknown: true, } )
Functions ¶
func FormMarshal ¶
FormMarshal marshals a value into a URL-encoded form.
See encoding/form.Marshal.
Example ¶
type Form struct {
Key string `form:"key"`
}
f := Form{
Key: "value",
}
data, _ := FormMarshal(f)
fmt.Println(string(data))
Output: key=value
func FormUnmarshal ¶
FormUnmarshal unmarshals a URL-encoded form into a value.
See encoding/form.Unmarshal.
Example ¶
type Form struct {
Key string `form:"key"`
}
data := []byte("key=value")
f := Form{}
_ = FormUnmarshal(data, &f)
fmt.Println(f.Key)
Output: value
func JsonMarshal ¶
JsonMarshal marshals v to a JSON encoded byte slice, which v can be a proto.Message. If v is a proto.Message, it will be marshaled to a JSON encoded byte slice by the protojson.MarshalOptions.
兼容 protobuf message 的 json.Marshaler 方法
Example ¶
type Json struct {
Key string `json:"key"`
}
j := Json{
Key: "value",
}
data, _ := JsonMarshal(j)
fmt.Println(string(data))
Output: {"key":"value"}
func JsonUnmarshal ¶
JsonUnmarshal unmarshal data to v, which v can be a proto.Message.
兼容 protobuf message 的 json.Unmarshaler 方法
func ProtoMarshal ¶
ProtoMarshal marshal protobuf message. Note JSON marshal use JsonMarshal.
Protobuf 消息编码,注意 JSON 格式编码使用 JsonMarshal。
func ProtoUnmarshal ¶
ProtoUnmarshal unmarshal protobuf message. Note JSON unmarshal use JsonUnmarshal.
Protobuf 消息解码,注意 JSON 格式解码使用 JsonUnmarshal。
func YamlUnmarshal ¶
YamlUnmarshal is a shortcut to yaml.Unmarshal
YamlUnmarshal 是 yaml.Unmarshal 的快捷方式
Types ¶
This section is empty.