Documentation
¶
Index ¶
- func Marshal(v any) (data []byte, err error)
- func MarshalTo(v any, w io.Writer) (err error)
- func Unmarshal(data []byte, v any) (err error)
- func UnmarshalFrom(r io.Reader, v any) (err error)
- type Decoder
- func (d *Decoder) ReadBool(data *bool, tag byte, require bool) (err error)
- func (d *Decoder) ReadFloat32(data *float32, tag byte, require bool) (err error)
- func (d *Decoder) ReadFloat64(data *float64, tag byte, require bool) (err error)
- func (d *Decoder) ReadHead(tag byte, require bool) (t JceEncodeType, have bool, err error)
- func (d *Decoder) ReadInt16(data *int16, tag byte, require bool) (err error)
- func (d *Decoder) ReadInt32(data *int32, tag byte, require bool) (err error)
- func (d *Decoder) ReadInt64(data *int64, tag byte, require bool) (err error)
- func (d *Decoder) ReadInt8(data *int8, tag byte, require bool) (err error)
- func (d *Decoder) ReadLength() (length uint32, err error)
- func (d *Decoder) ReadSliceInt8(data *[]int8, tag byte, require bool) (err error)
- func (d *Decoder) ReadSliceUint8(data *[]uint8, tag byte, require bool) (err error)
- func (d *Decoder) ReadString(data *string, tag byte, require bool) (err error)
- func (d *Decoder) ReadStructBegin() (err error)
- func (d *Decoder) ReadStructEnd() (err error)
- func (d *Decoder) ReadUint16(data *uint16, tag byte, require bool) (err error)
- func (d *Decoder) ReadUint32(data *uint32, tag byte, require bool) (err error)
- func (d *Decoder) ReadUint64(data *uint64, tag byte, require bool) (err error)
- func (d *Decoder) ReadUint8(data *uint8, tag byte, require bool) (err error)
- func (d *Decoder) Reader() (reader *bufio.Reader)
- type Encoder
- func (e *Encoder) Flush() (err error)
- func (e *Encoder) WriteBool(data bool, tag byte) (err error)
- func (e *Encoder) WriteFloat32(data float32, tag byte) (err error)
- func (e *Encoder) WriteFloat64(data float64, tag byte) (err error)
- func (e *Encoder) WriteHead(t JceEncodeType, tag byte) (err error)
- func (e *Encoder) WriteInt16(data int16, tag byte) (err error)
- func (e *Encoder) WriteInt32(data int32, tag byte) (err error)
- func (e *Encoder) WriteInt64(data int64, tag byte) (err error)
- func (e *Encoder) WriteInt8(data int8, tag byte) (err error)
- func (e *Encoder) WriteLength(length uint32) (err error)
- func (e *Encoder) WriteSliceInt8(data []int8, tag byte) (err error)
- func (e *Encoder) WriteSliceUint8(data []uint8, tag byte) (err error)
- func (e *Encoder) WriteString(data string, tag byte) (err error)
- func (e *Encoder) WriteStructBegin() (err error)
- func (e *Encoder) WriteStructEnd() (err error)
- func (e *Encoder) WriteUint16(data uint16, tag byte) (err error)
- func (e *Encoder) WriteUint32(data uint32, tag byte) (err error)
- func (e *Encoder) WriteUint64(data uint64, tag byte) (err error)
- func (e *Encoder) WriteUint8(data uint8, tag byte) (err error)
- func (e *Encoder) Writer() (writer *bufio.Writer)
- type JceEncodeType
- type Messager
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
func NewDecoder ¶
func (*Decoder) ReadFloat32 ¶
反序列化 float32
func (*Decoder) ReadFloat64 ¶
反序列化 float64
func (*Decoder) ReadHead ¶
根据 tag、require 读取对应数据的 type 传入 tag 和是否一定的存在 返回读取的结果 type,以及 tag 是否存在,最后是是否存在错误
func (*Decoder) ReadSliceInt8 ¶
反序列化 []int8
func (*Decoder) ReadSliceUint8 ¶
反序列化 []uint8
func (*Decoder) ReadString ¶
反序列化 string
func (*Decoder) ReadStructBegin ¶
read struct begin type
func (*Decoder) ReadUint16 ¶
反序列化 uint16
func (*Decoder) ReadUint32 ¶
反序列化 uint32
func (*Decoder) ReadUint64 ¶
反序列化 uint64
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder 编码器,用于序列化
func NewEncoder ¶
func (*Encoder) WriteFloat32 ¶
序列化 float32
func (*Encoder) WriteFloat64 ¶
序列化 float64
func (*Encoder) WriteHead ¶
func (e *Encoder) WriteHead(t JceEncodeType, tag byte) (err error)
序列化 head,即 type+tag 方案如下: 1. 如果 tag < 15, 则编码为: ------------------- | Type | Tag | | 4 bits | 4 bits | -------------------
2. 如果 tag >= 15, 则编码为: ---------------------------- | Type | Tag 1 | Tag 2 | | 4 bits | 4 bits | 1 byte | ---------------------------- 其中 tag1 存默认值 15,真正的 tag 值存于 tag2 位置
为什么要像上面这样设计?而不是直接 type、tag 分别两个字节? 主要是考虑到 tag 很可能没有 15 大,只需 4bit 就能编码,而不用 8bit,同时 type 也 4bit 就能放下,那么 总的其实 1Byte 就能存,所以就根据 tag 的大小进行了位的压缩
func (*Encoder) WriteInt16 ¶
序列化 int16
func (*Encoder) WriteInt32 ¶
序列化 int32
func (*Encoder) WriteInt64 ¶
序列化 int64
func (*Encoder) WriteInt8 ¶
序列化 int8 方案如下: |----------------------| | type | tag | data | |----------------------|
func (*Encoder) WriteSliceInt8 ¶
[]int8 类型的序列化,同 []uint8
func (*Encoder) WriteSliceUint8 ¶
[]uint8 类型的序列化,方案如下: ---------------------------------------------------- | simpleList head | data length | data type | data | ----------------------------------------------------
func (*Encoder) WriteString ¶
主要是 vector<string> 这种情况,写内部 string 时,也每次都写了个 tag,都默认是 0,感觉不太好,这个是无效信息 序列化 string 方案如下: |---------------------------------------| | type | tag | length(1B or 4B) | data | |---------------------------------------| 注意点在于根据长度选择 length 字段的字节数,这个主要是进行了优化
func (*Encoder) WriteStructBegin ¶
write struct begin type
func (*Encoder) WriteStructEnd ¶
write struct end type
func (*Encoder) WriteUint16 ¶
序列化 uint16
func (*Encoder) WriteUint32 ¶
序列化 uint32
func (*Encoder) WriteUint64 ¶
序列化 uint64
func (*Encoder) WriteUint8 ¶
序列化 uint8
type JceEncodeType ¶
type JceEncodeType byte
jce 基础编码类型表,用来编码使用,和语言无关
const ( Int1 JceEncodeType = iota Int2 Int4 Int8 Float4 Float8 Zero String Map SimpleList List StructBegin StructEnd )
jce type
func (JceEncodeType) String ¶
func (j JceEncodeType) String() string