io

package
v3.0.16 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: MIT Imports: 18 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// Serialize Type.
	TagInteger  byte = 'i'
	TagLong     byte = 'l'
	TagDouble   byte = 'd'
	TagNull     byte = 'n'
	TagEmpty    byte = 'e'
	TagTrue     byte = 't'
	TagFalse    byte = 'f'
	TagNaN      byte = 'N'
	TagInfinity byte = 'I'
	TagDate     byte = 'D'
	TagTime     byte = 'T'
	TagUTC      byte = 'Z'
	TagBytes    byte = 'b'
	TagUTF8Char byte = 'u'
	TagString   byte = 's'
	TagGUID     byte = 'g'
	TagList     byte = 'a'
	TagMap      byte = 'm'
	TagClass    byte = 'c'
	TagObject   byte = 'o'
	TagRef      byte = 'r'

	// Serialize Marks.
	TagPos        byte = '+'
	TagNeg        byte = '-'
	TagSemicolon  byte = ';'
	TagOpenbrace  byte = '{'
	TagClosebrace byte = '}'
	TagQuote      byte = '"'
	TagPoint      byte = '.'

	// Protocol Tags.
	TagHeader byte = 'H'
	TagCall   byte = 'C'
	TagResult byte = 'R'
	TagError  byte = 'E'
	TagEnd    byte = 'z'
)

Hprose Tags.

Variables

View Source
var ErrInvalidUTF8 = errors.New("hprose/io: invalid UTF-8")

ErrInvalidUTF8 means that a decoder encountered invalid UTF-8.

Functions

func AppendInt64

func AppendInt64(buf []byte, i int64) []byte

AppendInt64 i to buf.

func AppendUint64

func AppendUint64(buf []byte, i uint64) []byte

AppendUint64 i to buf.

func Convert

func Convert(src interface{}, t reflect.Type) (interface{}, error)

func FreeDecoder added in v3.0.5

func FreeDecoder(decoder *Decoder)

func FreeEncoder added in v3.0.5

func FreeEncoder(encoder *Encoder)

func GetConverter

func GetConverter(src, dest reflect.Type) func(dec *Decoder, o interface{}, p interface{})

GetConverter returns the converter for converting src to dest.

func GetStructType

func GetStructType(alias string) reflect.Type

GetStructType by alias.

func Marshal

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

func Register

func Register(proto interface{}, tag ...string)

Register the type of the proto with tag.

func RegisterConverter

func RegisterConverter(src, dest reflect.Type, converter func(dec *Decoder, o interface{}, p interface{}))

RegisterConverter for converting src to dest.

func RegisterName

func RegisterName(alias string, proto interface{}, tag ...string)

RegisterName the type of the proto with name & tag.

func RegisterValueDecoder

func RegisterValueDecoder(v interface{}, valdec ValueDecoder)

RegisterValueDecoder valdec.

func RegisterValueEncoder

func RegisterValueEncoder(v interface{}, valenc ValueEncoder)

RegisterValueEncoder of type(v).

func Unmarshal

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

func UnmarshalFromReader added in v3.0.5

func UnmarshalFromReader(reader io.Reader, v interface{}) error

Types

type CastError

type CastError struct {
	Source      reflect.Type
	Destination reflect.Type
}

A CastError is returned by Decoder when can not cast source type to destination type.

func (CastError) Error

func (e CastError) Error() string

type DecodeError

type DecodeError string

DecodeError is returned by Decoder when the data is wrong.

func (DecodeError) Error

func (e DecodeError) Error() string

type DecodeHandler

type DecodeHandler func(dec *Decoder, t reflect.Type, p unsafe.Pointer)

DecodeHandler is an decode handler.

func GetDecodeHandler

func GetDecodeHandler(t reflect.Type) DecodeHandler

GetDecodeHandler for specified type.

type Decoder

type Decoder struct {
	Error error
	LongType
	RealType
	MapType
	StructType
	ListType
	// contains filtered or unexported fields
}

Decoder is a io.Reader like object, with hprose specific read functions. Error is not returned as return value, but stored as Error member on this decoder instance.

func GetDecoder added in v3.0.5

func GetDecoder() *Decoder

func NewDecoder

func NewDecoder(input []byte) *Decoder

NewDecoder creates an Decoder instance from byte array.

func NewDecoderFromReader

func NewDecoderFromReader(reader io.Reader, bufSize ...int) *Decoder

NewDecoderFromReader creates an Decoder instance from io.Reader.

func (*Decoder) AddReference

func (dec *Decoder) AddReference(o interface{})

AddReference adds o to the reference.

func (*Decoder) Decode

func (dec *Decoder) Decode(p interface{}, tag ...byte)

Decode a data from the Decoder.

func (*Decoder) IsSimple

func (dec *Decoder) IsSimple() bool

IsSimple returns the decoder is in simple mode or not.

func (*Decoder) LastReferenceIndex

func (dec *Decoder) LastReferenceIndex() int

LastReferenceIndex returns the last index of the reference.

func (*Decoder) Next

func (dec *Decoder) Next(n int) []byte

Next returns a slice containing the next n bytes from the buffer, advancing the buffer as if the bytes had been returned by Read. If there are fewer than n bytes in the buffer, Next returns the entire buffer. The returned slice is always valid.

func (*Decoder) NextByte

func (dec *Decoder) NextByte() (b byte)

NextByte reads and returns the next byte from the dec. If no byte is available, it returns 0.

func (*Decoder) Read

func (dec *Decoder) Read(t reflect.Type, tag ...byte) (result interface{})

Read returns a data of the specified type from the Decoder.

func (*Decoder) ReadBigFloat

func (dec *Decoder) ReadBigFloat() *big.Float

ReadBigFloat reads *big.Float.

func (*Decoder) ReadBigInt

func (dec *Decoder) ReadBigInt() *big.Int

ReadBigInt reads *big.Int.

func (*Decoder) ReadBytes

func (dec *Decoder) ReadBytes() []byte

ReadBytes reads bytes and add reference.

func (*Decoder) ReadDateTime

func (dec *Decoder) ReadDateTime() (t time.Time)

ReadDateTime reads time.Time and add reference.

func (*Decoder) ReadFloat32

func (dec *Decoder) ReadFloat32() (value float32)

ReadFloat32 reads float32.

func (*Decoder) ReadFloat64

func (dec *Decoder) ReadFloat64() (value float64)

ReadFloat64 reads float64.

func (*Decoder) ReadInt

func (dec *Decoder) ReadInt() (value int)

ReadInt reads int.

func (*Decoder) ReadInt16

func (dec *Decoder) ReadInt16() (value int16)

ReadInt16 reads int16.

func (*Decoder) ReadInt32

func (dec *Decoder) ReadInt32() (value int32)

ReadInt32 reads int32.

func (*Decoder) ReadInt64

func (dec *Decoder) ReadInt64() (value int64)

ReadInt64 reads int64.

func (*Decoder) ReadInt8

func (dec *Decoder) ReadInt8() (value int8)

ReadInt8 reads int8.

func (*Decoder) ReadObject

func (dec *Decoder) ReadObject() interface{}

ReadObject reads object and add reference.

func (*Decoder) ReadReference

func (dec *Decoder) ReadReference(p interface{})

ReadReference to p.

func (*Decoder) ReadSafeString

func (dec *Decoder) ReadSafeString() (s string)

ReadSafeString reads safe string.

func (*Decoder) ReadString

func (dec *Decoder) ReadString() (s string)

ReadString reads safe string and add reference.

func (*Decoder) ReadStringAsBytes

func (dec *Decoder) ReadStringAsBytes() (data []byte)

ReadStringAsBytes reads string as bytes.

func (*Decoder) ReadStruct

func (dec *Decoder) ReadStruct(t reflect.Type)

ReadStruct reads struct type.

func (*Decoder) ReadTime

func (dec *Decoder) ReadTime() (t time.Time)

ReadTime reads time.Time and add reference.

func (*Decoder) ReadUUID

func (dec *Decoder) ReadUUID() uuid.UUID

ReadUUID reads uuid.UUID and add reference.

func (*Decoder) ReadUint

func (dec *Decoder) ReadUint() (value uint)

ReadUint reads uint.

func (*Decoder) ReadUint16

func (dec *Decoder) ReadUint16() (value uint16)

ReadUint16 reads uint16.

func (*Decoder) ReadUint32

func (dec *Decoder) ReadUint32() (value uint32)

ReadUint32 reads uint32.

func (*Decoder) ReadUint64

func (dec *Decoder) ReadUint64() (value uint64)

ReadUint64 reads uint64.

func (*Decoder) ReadUint8

func (dec *Decoder) ReadUint8() (value uint8)

ReadUint8 reads uint8.

func (*Decoder) ReadUnsafeString

func (dec *Decoder) ReadUnsafeString() (s string)

ReadUnsafeString reads unsafe string.

func (*Decoder) Remains

func (dec *Decoder) Remains() (data []byte)

Remains reads and returns all bytes data in this iter that has not been read.

func (*Decoder) Reset

func (dec *Decoder) Reset() *Decoder

Reset the value reference and struct type reference.

func (*Decoder) ResetBuffer added in v3.0.5

func (dec *Decoder) ResetBuffer() *Decoder

ResetBuffer of the Decoder.

func (*Decoder) ResetBytes

func (dec *Decoder) ResetBytes(input []byte) *Decoder

ResetBytes reuse decoder instance by specifying another byte array as input.

func (*Decoder) ResetReader

func (dec *Decoder) ResetReader(reader io.Reader) *Decoder

ResetReader reuse decoder instance by specifying another reader.

func (*Decoder) SetReference

func (dec *Decoder) SetReference(i int, o interface{})

SetReference sets o to the reference at index i.

func (*Decoder) Simple

func (dec *Decoder) Simple(simple bool) *Decoder

Simple resets the decoder to simple mode or not.

func (*Decoder) Skip

func (dec *Decoder) Skip()

Skip the next byte from the dec.

func (*Decoder) UnsafeNext

func (dec *Decoder) UnsafeNext(n int) (data []byte)

UnsafeNext returns a slice containing the next n bytes from the buffer, advancing the buffer as if the bytes had been returned by Read. If there are fewer than n bytes in the buffer, Next returns the entire buffer. The returned slice is only valid until the next call to a read method.

func (*Decoder) UnsafeUntil

func (dec *Decoder) UnsafeUntil(delim byte) (data []byte)

UnsafeUntil reads until the first occurrence of delim in the input, returning a slice containing the data up to and not including the delimiter. The returned slice is only valid until the next call to a read method.

func (*Decoder) Until

func (dec *Decoder) Until(delim byte) []byte

Until reads until the first occurrence of delim in the input, returning a slice containing the data up to and not including the delimiter. The returned slice is always valid.

type EncodeHandler

type EncodeHandler func(enc *Encoder, v interface{})

EncodeHandler is an encode handler.

func GetEncodeHandler

func GetEncodeHandler(t reflect.Type) (handler EncodeHandler)

GetEncodeHandler for specified type.

type Encoder

type Encoder struct {
	Writer io.Writer
	Error  error
	// contains filtered or unexported fields
}

An Encoder writes hprose data to an output stream.

func GetEncoder added in v3.0.5

func GetEncoder() *Encoder

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder create an encoder object.

func (*Encoder) AddReferenceCount

func (enc *Encoder) AddReferenceCount(n int)

AddReferenceCount n.

func (*Encoder) Buffer added in v3.0.5

func (enc *Encoder) Buffer() []byte

Buffer returns the accumulated bytes.

func (*Encoder) Bytes

func (enc *Encoder) Bytes() []byte

Bytes returns a copy of the accumulated bytes.

func (*Encoder) Encode

func (enc *Encoder) Encode(v interface{}) (err error)

Encode writes the hprose io of v to stream. If v is already written to stream, it will writes it as reference.

func (*Encoder) EncodeReference

func (enc *Encoder) EncodeReference(valenc ValueEncoder, v interface{})

EncodeReference to encoder.

func (*Encoder) EncodeString

func (enc *Encoder) EncodeString(s string)

EncodeString to encoder.

func (*Encoder) Flush

func (enc *Encoder) Flush() (err error)

Flush writes the io data from buf to Writer.

func (*Encoder) IsSimple

func (enc *Encoder) IsSimple() bool

IsSimple returns the encoder is in simple mode or not.

func (*Encoder) Reset

func (enc *Encoder) Reset() *Encoder

Reset the value reference and struct type reference.

func (*Encoder) ResetBuffer added in v3.0.5

func (enc *Encoder) ResetBuffer() *Encoder

ResetBuffer of the Encoder.

func (*Encoder) SetReference

func (enc *Encoder) SetReference(v interface{})

SetReference of v.

func (*Encoder) SetStringReference

func (enc *Encoder) SetStringReference(s string)

SetStringReference of v.

func (*Encoder) Simple

func (enc *Encoder) Simple(simple bool) *Encoder

Simple resets the encoder to simple mode or not.

func (*Encoder) String

func (enc *Encoder) String() string

String returns a copy of the accumulated string.

func (*Encoder) UnsafeString added in v3.0.5

func (enc *Encoder) UnsafeString() string

UnsafeString returns the accumulated string.

func (*Encoder) Write

func (enc *Encoder) Write(v interface{}) (err error)

Write writes the hprose io of v to stream. If v is already written to stream, it will writes it as value.

func (*Encoder) WriteArray

func (enc *Encoder) WriteArray(v interface{})

WriteArray to encoder.

func (*Encoder) WriteBigFloat

func (enc *Encoder) WriteBigFloat(f *big.Float)

WriteBigFloat to encoder.

func (*Encoder) WriteBigInt

func (enc *Encoder) WriteBigInt(i *big.Int)

WriteBigInt to encoder.

func (*Encoder) WriteBigRat

func (enc *Encoder) WriteBigRat(r *big.Rat)

WriteBigRat to encoder.

func (*Encoder) WriteBool

func (enc *Encoder) WriteBool(b bool)

WriteBool to encoder.

func (*Encoder) WriteComplex128

func (enc *Encoder) WriteComplex128(c complex128)

WriteComplex128 to encoder.

func (*Encoder) WriteComplex64

func (enc *Encoder) WriteComplex64(c complex64)

WriteComplex64 to encoder.

func (*Encoder) WriteError

func (enc *Encoder) WriteError(e error)

WriteError to encoder.

func (*Encoder) WriteFloat32

func (enc *Encoder) WriteFloat32(f float32)

WriteFloat32 to encoder.

func (*Encoder) WriteFloat64

func (enc *Encoder) WriteFloat64(f float64)

WriteFloat64 to encoder.

func (*Encoder) WriteFoot

func (enc *Encoder) WriteFoot()

WriteFoot of list or map to encoder.

func (*Encoder) WriteInt

func (enc *Encoder) WriteInt(i int)

WriteInt to encoder.

func (*Encoder) WriteInt16

func (enc *Encoder) WriteInt16(i int16)

WriteInt16 to encoder.

func (*Encoder) WriteInt32

func (enc *Encoder) WriteInt32(i int32)

WriteInt32 to encoder.

func (*Encoder) WriteInt64

func (enc *Encoder) WriteInt64(i int64)

WriteInt64 to encoder.

func (*Encoder) WriteInt8

func (enc *Encoder) WriteInt8(i int8)

WriteInt8 to encoder.

func (*Encoder) WriteListHead

func (enc *Encoder) WriteListHead(n int)

WriteListHead to encoder, n is the count of elements in list.

func (*Encoder) WriteMap

func (enc *Encoder) WriteMap(v interface{})

WriteMap to encoder.

func (*Encoder) WriteMapHead

func (enc *Encoder) WriteMapHead(n int)

WriteMapHead to encoder, n is the count of elements in map.

func (*Encoder) WriteNil

func (enc *Encoder) WriteNil()

WriteNil to encoder.

func (*Encoder) WriteObjectHead

func (enc *Encoder) WriteObjectHead(r int)

WriteObjectHead to encoder, r is the reference number of struct.

func (*Encoder) WriteReference

func (enc *Encoder) WriteReference(v interface{}) bool

WriteReference of v to stream.

func (*Encoder) WriteSlice

func (enc *Encoder) WriteSlice(v interface{})

WriteSlice to encoder.

func (*Encoder) WriteString

func (enc *Encoder) WriteString(s string)

WriteString to encoder.

func (*Encoder) WriteStringReference

func (enc *Encoder) WriteStringReference(s string) bool

WriteStringReference of v to stream.

func (*Encoder) WriteStructType

func (enc *Encoder) WriteStructType(t reflect.Type, action func()) (r int)

WriteStructType of t to stream with action.

func (*Encoder) WriteTag

func (enc *Encoder) WriteTag(tag byte)

WriteTag to encoder.

func (*Encoder) WriteUint

func (enc *Encoder) WriteUint(i uint)

WriteUint to encoder.

func (*Encoder) WriteUint16

func (enc *Encoder) WriteUint16(i uint16)

WriteUint16 to encoder.

func (*Encoder) WriteUint32

func (enc *Encoder) WriteUint32(i uint32)

WriteUint32 to encoder.

func (*Encoder) WriteUint64

func (enc *Encoder) WriteUint64(i uint64)

WriteUint64 to encoder.

func (*Encoder) WriteUint8

func (enc *Encoder) WriteUint8(i uint8)

WriteUint8 to encoder.

type FieldAccessor

type FieldAccessor struct {
	Type   reflect2.Type
	Alias  string
	Field  reflect2.StructField
	Encode EncodeHandler
	Decode DecodeHandler
}

FieldAccessor .

type Formatter

type Formatter struct {
	Simple bool
	LongType
	RealType
	MapType
}

func (Formatter) Marshal

func (f Formatter) Marshal(v interface{}) ([]byte, error)

func (Formatter) Unmarshal

func (f Formatter) Unmarshal(data []byte, v interface{}) error

func (Formatter) UnmarshalFromReader added in v3.0.5

func (f Formatter) UnmarshalFromReader(reader io.Reader, v interface{}) error

type ListType added in v3.0.15

type ListType int8
const (
	// ListTypeISlice represents the default type is []interface{}.
	ListTypeISlice ListType = iota
	// ListTypeSlice represents the default type is []T.
	ListTypeSlice
)

type LongType

type LongType int8

LongType represents the default type for decode long integer.

const (
	// LongTypeInt represents the default type is int.
	LongTypeInt LongType = iota
	// LongTypeUint represents the default type is uint.
	LongTypeUint
	// LongTypeInt64 represents the default type is int64.
	LongTypeInt64
	// LongTypeUint64 represents the default type is uint64.
	LongTypeUint64
	// LongTypeBigInt represents the default type is *big.Int.
	LongTypeBigInt
)

type MapType

type MapType int8

MapType represents the default type for decode map.

const (
	// MapTypeIIMap represents the default type is map[interface{}]interface{}.
	MapTypeIIMap MapType = iota
	// MapTypeSIMap represents the default type is map[string]interface{}.
	MapTypeSIMap
)

type RealType

type RealType int8

RealType represents the default type for decode real number.

const (
	// RealTypeFloat64 represents the default type is float64.
	RealTypeFloat64 RealType = iota
	// RealTypeFloat32 represents the default type is float32.
	RealTypeFloat32
	// RealTypeBigFloat represents the default type is *big.Float.
	RealTypeBigFloat
)

type StructType added in v3.0.15

type StructType int8
const (
	// StructTypePtr represents the default type is *T.
	StructTypePtr StructType = iota
	// StructTypeValue represents the default type is T.
	StructTypeValue
)

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

An UnsupportedTypeError is returned by Encoder when attempting to encode an unsupported value type.

func (UnsupportedTypeError) Error

func (e UnsupportedTypeError) Error() string

type ValueDecoder

type ValueDecoder interface {
	Decode(dec *Decoder, p interface{}, tag byte)
}

ValueDecoder is the interface that groups the basic Decode methods.

func GetValueDecoder

func GetValueDecoder(v interface{}) (valdec ValueDecoder)

GetValueDecoder of Type t.

type ValueEncoder

type ValueEncoder interface {
	Encode(enc *Encoder, v interface{})
	Write(enc *Encoder, v interface{})
}

ValueEncoder is the interface that groups the basic Write and Encode methods.

func GetValueEncoder

func GetValueEncoder(v interface{}) ValueEncoder

GetValueEncoder of type(v).

Jump to

Keyboard shortcuts

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