phpserialize

package
v0.0.0-...-386a0b5 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodePHPString

func DecodePHPString(data []byte) string

DecodePHPString converts a string of ASCII bytes (like "Bj\xc3\xb6rk") back into a UTF8 string ("Björk", in that case).

func Marshal

func Marshal(input interface{}, options *MarshalOptions) ([]byte, error)

Marshal is the canonical way to perform the equivalent of serialize() in PHP. It can handle encoding scalar types, slices and maps.

func MarshalBool

func MarshalBool(value bool) []byte

MarshalBool returns the bytes to represent a PHP serialized bool value. This would be the equivalent to running:

echo serialize(false);
// b:0;

The same result would be returned by marshalling a boolean value:

Marshal(true)

func MarshalBytes

func MarshalBytes(value []byte) []byte

MarshalBytes returns the bytes to represent a PHP serialized string value that contains binary data. This is because PHP does not have a distinct type for binary data.

This can cause some confusion when decoding the value as it will want to unmarshal as a string type. The Unmarshal() function will be sensitive to this condition and allow either a string or []byte when unserializing a PHP string.

func MarshalFloat

func MarshalFloat(value float64, bitSize int) []byte

MarshalFloat returns the bytes to represent a PHP serialized floating-point value. This would be the equivalent to running:

echo serialize(1.23);
// d:1.23;

The bitSize should represent the size of the float. This makes conversion to a string value more accurate, for example:

// float64 is implicit for literals
MarshalFloat(1.23, 64)

// If the original value was cast from a float32
f := float32(1.23)
MarshalFloat(float64(f), 32)

The same result would be returned by marshalling a floating-point value:

Marshal(1.23)

func MarshalInt

func MarshalInt(value int64) []byte

MarshalInt returns the bytes to represent a PHP serialized integer value. This would be the equivalent to running:

echo serialize(123);
// i:123;

The same result would be returned by marshalling an integer value:

Marshal(123)

func MarshalNil

func MarshalNil() []byte

MarshalNil returns the bytes to represent a PHP serialized null value. This would be the equivalent to running:

echo serialize(null);
// N;

Unlike the other specific Marshal functions it does not take an argument because the output is a constant value.

func MarshalString

func MarshalString(value string) []byte

MarshalString returns the bytes to represent a PHP serialized string value. This would be the equivalent to running:

echo serialize('Hello world');
// s:11:"Hello world";

The same result would be returned by marshalling a string value:

Marshal('Hello world')

One important distinction is that PHP stores binary data in strings. See MarshalBytes for more information.

func MarshalStruct

func MarshalStruct(input interface{}, options *MarshalOptions) ([]byte, error)

MarshalStruct returns the bytes that represent a PHP encoded class from a struct or pointer to a struct.

Fields that are not exported (starting with a lowercase letter) will not be present in the output. All fields that appear in the output will have their first letter converted to lowercase. Any other uppercase letters in the field name are maintained. At the moment there is no way to change this behaviour, unlike other marshallers that use a tag on the field.

func MarshalUint

func MarshalUint(value uint64) []byte

MarshalUint is provided for compatibility with unsigned types in Go. It works the same way as MarshalInt.

func StringifyKeys

func StringifyKeys(m map[interface{}]interface{}) (out map[string]interface{})

StringifyKeys recursively converts a map into a more sensible map with strings as keys.

map[interface{}]interface{} is used as an unmarshalling format because PHP serialise() permits keys of associative arrays to be non-string. However, in reality this is rarely the case and so strings for keys are much more compatible with external code.

func Unmarshal

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

func UnmarshalAssociativeArray

func UnmarshalAssociativeArray(data []byte) (map[interface{}]interface{}, error)

func UnmarshalBool

func UnmarshalBool(data []byte) (bool, error)

func UnmarshalBytes

func UnmarshalBytes(data []byte) ([]byte, error)

func UnmarshalFloat

func UnmarshalFloat(data []byte) (float64, error)

func UnmarshalIndexedArray

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

func UnmarshalInt

func UnmarshalInt(data []byte) (int64, error)

func UnmarshalNil

func UnmarshalNil(data []byte) error

func UnmarshalObject

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

func UnmarshalString

func UnmarshalString(data []byte) (string, error)

func UnmarshalUint

func UnmarshalUint(data []byte) (uint64, error)

Types

type MarshalOptions

type MarshalOptions struct {
	// If this is true, then all struct names will be stripped from objects
	// and "stdClass" will be used instead. The default value is false.
	OnlyStdClass bool
}

MarshalOptions must be provided when invoking Marshal(). Use DefaultMarshalOptions() for sensible defaults.

func DefaultMarshalOptions

func DefaultMarshalOptions() *MarshalOptions

DefaultMarshalOptions will create a new instance of MarshalOptions with sensible defaults. See MarshalOptions for a full description of options.

Jump to

Keyboard shortcuts

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