Documentation
¶
Overview ¶
Package phpserialize implements serialization and deserialization of the PHP serialize() data format.
The package is organized into four independent layers:
- Parser (parser.go): PHP serialized bytes -> Value
- Writer (writer.go): Value -> PHP serialized bytes
- Encode (encode.go): Go value -> Value (via reflection)
- Decode (decode.go): Value -> Go value (via reflection)
The Value type is the intermediate representation shared by all four layers. The parser and writer never inspect Go destination types, and the encoder and decoder never touch raw bytes; reflection lives only in the encode and decode layers.
The high-level entry points Marshal and Unmarshal mirror the API of the standard library's encoding/json package.
Index ¶
- Variables
- func Decode(v Value, dst any) error
- func Marshal(v any) ([]byte, error)
- func Unmarshal(data []byte, v any) error
- func Write(v Value) ([]byte, error)
- type Entry
- type Field
- type InvalidUnmarshalError
- type Kind
- type Object
- type PHPClass
- type SyntaxError
- type UnmarshalTypeError
- type UnsupportedTypeError
- type UnsupportedValueError
- type Value
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupportedType = errors.New("phpserialize: unsupported type")
ErrUnsupportedType is returned by the parser when it encounters a PHP serialized type that this package deliberately does not support: custom serialized objects ("C") and references ("R"/"r").
Functions ¶
func Decode ¶
Decode fills the destination pointed to by dst with the contents of the intermediate Value using reflection. It is the only decoding layer that inspects Go types; it never parses raw bytes.
dst must be a non-nil pointer; otherwise an InvalidUnmarshalError is returned. Decoding into an interface value uses the default mapping described on Unmarshal.
func Marshal ¶
Marshal returns the PHP serialized encoding of v.
It is a convenience wrapper that first encodes v into the intermediate Value representation with Encode and then serializes that Value to bytes with Write. See Encode for the Go-to-PHP type mapping.
func Unmarshal ¶
Unmarshal parses the PHP serialized data and stores the result in the value pointed to by v.
It is a convenience wrapper that first parses data into the intermediate Value representation with Parse and then decodes that Value into v with Decode. v must be a non-nil pointer. See Decode for the PHP-to-Go type mapping, including how PHP values are decoded into an empty interface.
Types ¶
type InvalidUnmarshalError ¶
InvalidUnmarshalError describes an invalid argument passed to Unmarshal or Decode. The argument must be a non-nil pointer.
func (*InvalidUnmarshalError) Error ¶
func (e *InvalidUnmarshalError) Error() string
type Kind ¶
type Kind uint8
Kind identifies the PHP type carried by a Value.
const ( // NullKind represents the PHP null value ("N;"). NullKind Kind = iota // BoolKind represents a PHP boolean ("b:0;" or "b:1;"). BoolKind // IntKind represents a PHP integer ("i:<n>;"). IntKind // FloatKind represents a PHP double ("d:<n>;"). FloatKind // StringKind represents a PHP string ("s:<len>:\"...\";"). StringKind // ArrayKind represents a PHP array ("a:<n>:{...}"). ArrayKind // ObjectKind represents a PHP object ("O:<len>:\"class\":<n>:{...}"). ObjectKind )
The set of PHP types representable by a Value.
type Object ¶
Object is the payload of an ObjectKind value. It records the PHP class name and the object's fields in serialization order.
type PHPClass ¶
type PHPClass interface {
// PHPClassName returns the PHP class name to use when serializing the
// implementing value as a PHP object.
PHPClassName() string
}
PHPClass may be implemented by a Go type to control how it is encoded. When a value's type implements PHPClass, Encode serializes it as a PHP object ("O") using the returned class name, rather than as an associative array.
type SyntaxError ¶
type SyntaxError struct {
Offset int64
// contains filtered or unexported fields
}
SyntaxError describes malformed PHP serialized input. Offset records the byte position in the input where the error was detected.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
type UnmarshalTypeError ¶
type UnmarshalTypeError struct {
// Value is a description of the PHP value ("int", "string", ...).
Value string
// Type is the Go type that could not hold the PHP value.
Type reflect.Type
// Field is the struct field path being decoded, if any.
Field string
}
UnmarshalTypeError describes a PHP value that was not appropriate for a value of a specific Go type during decoding.
func (*UnmarshalTypeError) Error ¶
func (e *UnmarshalTypeError) Error() string
type UnsupportedTypeError ¶
UnsupportedTypeError is returned by Encode and Marshal when a Go value of a type that cannot be represented in the PHP serialized format is encountered (for example a channel or function).
func (*UnsupportedTypeError) Error ¶
func (e *UnsupportedTypeError) Error() string
type UnsupportedValueError ¶
UnsupportedValueError is returned by Encode and Marshal when a Go value cannot be represented in the PHP serialized format, such as a map with an unusable key type.
func (*UnsupportedValueError) Error ¶
func (e *UnsupportedValueError) Error() string
type Value ¶
type Value struct {
Kind Kind
Bool bool
Int int64
Float float64
String string
Array []Entry
Object *Object
}
Value is the intermediate representation of a single PHP value. It is a tagged union: the Kind field selects which of the remaining fields carries meaningful data.
- NullKind: no payload.
- BoolKind: Bool.
- IntKind: Int.
- FloatKind: Float.
- StringKind: String.
- ArrayKind: Array (ordered key/value entries).
- ObjectKind: Object (class name plus ordered fields).
func Encode ¶
Encode converts a Go value into the intermediate Value representation using reflection. It is the only encoding layer that inspects Go types; the writer operates purely on Value.
The mapping is:
- nil, nil pointers and nil interfaces -> PHP null.
- bool -> PHP bool.
- all integer and unsigned integer types -> PHP int.
- float32/float64 -> PHP double.
- string -> PHP string.
- []byte -> PHP string (raw bytes).
- slices and arrays -> sequential PHP array (keys 0..n-1).
- maps -> PHP array keyed by the map keys (keys sorted for determinism).
- structs -> associative PHP array, or a PHP object if the type implements PHPClass.
- pointers and interfaces -> the encoding of the value they refer to.
func Parse ¶
Parse parses PHP serialized bytes into a Value. It performs a strict recursive-descent parse and never inspects any Go destination type.
The entire input must be consumed by a single serialized value; trailing bytes are reported as a SyntaxError.