encoding

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2026 License: MIT, MIT Imports: 8 Imported by: 0

README

encoding

This package is copied and modified from kratos/encoding

此处代码从 kratos/encoding 拷贝并进行了修改。

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func FormMarshal(v interface{}) ([]byte, error)

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

func FormUnmarshal(data []byte, v interface{}) error

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

func JsonMarshal(v interface{}) ([]byte, error)

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

func JsonUnmarshal(data []byte, v interface{}) error

JsonUnmarshal unmarshal data to v, which v can be a proto.Message.

兼容 protobuf message 的 json.Unmarshaler 方法

func ProtoMarshal

func ProtoMarshal(v interface{}) ([]byte, error)

ProtoMarshal marshal protobuf message. Note JSON marshal use JsonMarshal.

Protobuf 消息编码,注意 JSON 格式编码使用 JsonMarshal。

func ProtoUnmarshal

func ProtoUnmarshal(data []byte, v interface{}) error

ProtoUnmarshal unmarshal protobuf message. Note JSON unmarshal use JsonUnmarshal.

Protobuf 消息解码,注意 JSON 格式解码使用 JsonUnmarshal。

func XmlMarshal

func XmlMarshal(v interface{}) ([]byte, error)

XmlMarshal is a shortcut of xml.Marshal

xml.Marshal 的快捷方式

func XmlUnmarshal

func XmlUnmarshal(data []byte, v interface{}) error

XmlUnmarshal is a shortcut of xml.Unmarshal

xml.Unmarshal 的快捷方式

func YamlMarshal

func YamlMarshal(v interface{}) ([]byte, error)

YamlMarshal is a shortcut to yaml.Marshal

YamlMarshal 是 yaml.Marshal 的快捷方式

func YamlUnmarshal

func YamlUnmarshal(data []byte, v interface{}) error

YamlUnmarshal is a shortcut to yaml.Unmarshal

YamlUnmarshal 是 yaml.Unmarshal 的快捷方式

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL