php_serialize

package
v0.0.0-...-a065a3b Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2018 License: BSD-2-Clause Imports: 5 Imported by: 31

README

PHP Serialize/Unserialize in Go/GoLang

This is the simple implementation of PHP serialize and unserialize functions written in Go/GoLang. This package was inspired by @yvasiyarov and improved by @fromYukki. Feel free to use it as you wish ;)

UnSerialize

decoder := NewUnSerializer("Some serialized string")
if val, err := decoder.Decode(); err != nil {
	panic(err)
} else {
	// val - is your PhpValue instance
}

Some details:

  • Any of PHP variable will be decoded as PhpValue type, and you need to cast it at your own type (int, string etc..);
  • Any integer may be converted to int (I'm sure that you know about 32 or 64 bits);
  • Any decimal my be converted to float64;
  • Any PHP arrays will be decoded as PhpArray type. This is the map of PhpValue All keys and values are PhpValue;
  • Any PHP objects will be decoded as PhpObject;
  • Any PHP objects that implement a Serializable interface wil be decoded as PhpObjectSerialized. Please remember it is not the same as PhpObject;
  • You can set your own unserialize function for objects that implement a Serializable interface by using SetSerializedDecodeFunc function.

Serialize

encoder := NewSerializer()
if val, err := encoder.Encode(source); err != nil {
	panic(err)
} else {
	// val - is your serialized string
}

Encode function expects PhpValue variable as argument.

TODO:

  • Write more informative README and some useful examples

Documentation

Index

Constants

View Source
const (
	TOKEN_NULL              rune = 'N'
	TOKEN_BOOL              rune = 'b'
	TOKEN_INT               rune = 'i'
	TOKEN_FLOAT             rune = 'd'
	TOKEN_STRING            rune = 's'
	TOKEN_ARRAY             rune = 'a'
	TOKEN_OBJECT            rune = 'O'
	TOKEN_OBJECT_SERIALIZED rune = 'C'
	TOKEN_REFERENCE         rune = 'R'
	TOKEN_REFERENCE_OBJECT  rune = 'r'
	TOKEN_SPL_ARRAY         rune = 'x'
	TOKEN_SPL_ARRAY_MEMBERS rune = 'm'

	SEPARATOR_VALUE_TYPE rune = ':'
	SEPARATOR_VALUES     rune = ';'

	DELIMITER_STRING_LEFT  rune = '"'
	DELIMITER_STRING_RIGHT rune = '"'
	DELIMITER_OBJECT_LEFT  rune = '{'
	DELIMITER_OBJECT_RIGHT rune = '}'

	FORMATTER_FLOAT     byte = 'g'
	FORMATTER_PRECISION int  = 17
)
View Source
const UNSERIALIZABLE_OBJECT_MAX_LEN = 10 * 1024 * 1024 * 1024

Variables

This section is empty.

Functions

func Debug

func Debug(value bool)

func PhpValueBool

func PhpValueBool(p PhpValue) (res bool)

func PhpValueFloat64

func PhpValueFloat64(p PhpValue) (res float64)

func PhpValueInt

func PhpValueInt(p PhpValue) (res int)

func PhpValueInt64

func PhpValueInt64(p PhpValue) (res int64)

func PhpValueString

func PhpValueString(p PhpValue) (res string)

func PhpValueUInt

func PhpValueUInt(p PhpValue) (res uint)

func PhpValueUInt64

func PhpValueUInt64(p PhpValue) (res uint64)

func Serialize

func Serialize(v PhpValue) (string, error)

Types

type PhpArray

type PhpArray map[PhpValue]PhpValue

type PhpObject

type PhpObject struct {
	// contains filtered or unexported fields
}

func NewPhpObject

func NewPhpObject(className string) *PhpObject

func (*PhpObject) GetClassName

func (self *PhpObject) GetClassName() string

func (*PhpObject) GetMembers

func (self *PhpObject) GetMembers() PhpArray

func (*PhpObject) GetPrivate

func (self *PhpObject) GetPrivate(name string) (v PhpValue, ok bool)

func (*PhpObject) GetProtected

func (self *PhpObject) GetProtected(name string) (v PhpValue, ok bool)

func (*PhpObject) GetPublic

func (self *PhpObject) GetPublic(name string) (v PhpValue, ok bool)

func (*PhpObject) SetClassName

func (self *PhpObject) SetClassName(name string) *PhpObject

func (*PhpObject) SetMembers

func (self *PhpObject) SetMembers(members PhpArray) *PhpObject

func (*PhpObject) SetPrivate

func (self *PhpObject) SetPrivate(name string, value PhpValue) *PhpObject

func (*PhpObject) SetProtected

func (self *PhpObject) SetProtected(name string, value PhpValue) *PhpObject

func (*PhpObject) SetPublic

func (self *PhpObject) SetPublic(name string, value PhpValue) *PhpObject

type PhpObjectSerialized

type PhpObjectSerialized struct {
	// contains filtered or unexported fields
}

func NewPhpObjectSerialized

func NewPhpObjectSerialized(className string) *PhpObjectSerialized

func (*PhpObjectSerialized) GetClassName

func (self *PhpObjectSerialized) GetClassName() string

func (*PhpObjectSerialized) GetData

func (self *PhpObjectSerialized) GetData() string

func (*PhpObjectSerialized) GetValue

func (self *PhpObjectSerialized) GetValue() PhpValue

func (*PhpObjectSerialized) SetClassName

func (self *PhpObjectSerialized) SetClassName(name string) *PhpObjectSerialized

func (*PhpObjectSerialized) SetData

func (self *PhpObjectSerialized) SetData(data string) *PhpObjectSerialized

func (*PhpObjectSerialized) SetValue

func (self *PhpObjectSerialized) SetValue(value PhpValue) *PhpObjectSerialized

type PhpSlice

type PhpSlice []PhpValue

type PhpSplArray

type PhpSplArray struct {
	// contains filtered or unexported fields
}

func NewPhpSplArray

func NewPhpSplArray(array, properties PhpValue) *PhpSplArray

func (*PhpSplArray) GetArray

func (self *PhpSplArray) GetArray() PhpValue

func (*PhpSplArray) GetFlags

func (self *PhpSplArray) GetFlags() int

func (*PhpSplArray) GetProperties

func (self *PhpSplArray) GetProperties() PhpValue

func (*PhpSplArray) SetArray

func (self *PhpSplArray) SetArray(value PhpValue)

func (*PhpSplArray) SetFlags

func (self *PhpSplArray) SetFlags(value int)

func (*PhpSplArray) SetProperties

func (self *PhpSplArray) SetProperties(value PhpValue)

type PhpValue

type PhpValue interface{}

func UnSerialize

func UnSerialize(s string) (PhpValue, error)

type SerializedDecodeFunc

type SerializedDecodeFunc func(string) (PhpValue, error)

type SerializedEncodeFunc

type SerializedEncodeFunc func(PhpValue) (string, error)

type Serializer

type Serializer struct {
	// contains filtered or unexported fields
}

func NewSerializer

func NewSerializer() *Serializer

func (*Serializer) Encode

func (self *Serializer) Encode(v PhpValue) (string, error)

func (*Serializer) SetSerializedEncodeFunc

func (self *Serializer) SetSerializedEncodeFunc(f SerializedEncodeFunc)

type UnSerializer

type UnSerializer struct {
	// contains filtered or unexported fields
}

func NewUnSerializer

func NewUnSerializer(data string) *UnSerializer

func (*UnSerializer) Decode

func (self *UnSerializer) Decode() (PhpValue, error)

func (*UnSerializer) SetReader

func (self *UnSerializer) SetReader(r *strings.Reader)

func (*UnSerializer) SetSerializedDecodeFunc

func (self *UnSerializer) SetSerializedDecodeFunc(f SerializedDecodeFunc)

Jump to

Keyboard shortcuts

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