Documentation ¶
Index ¶
- func DecodePHPString(data []byte) string
- func Marshal(input interface{}, options *MarshalOptions) ([]byte, error)
- func MarshalBool(value bool) []byte
- func MarshalBytes(value []byte) []byte
- func MarshalFloat(value float64, bitSize int) []byte
- func MarshalInt(value int64) []byte
- func MarshalNil() []byte
- func MarshalString(value string) []byte
- func MarshalStruct(input interface{}, options *MarshalOptions) ([]byte, error)
- func MarshalUint(value uint64) []byte
- func StringifyKeys(m map[interface{}]interface{}) (out map[string]interface{})
- func Unmarshal(data []byte, v interface{}) error
- func UnmarshalAssociativeArray(data []byte) (map[interface{}]interface{}, error)
- func UnmarshalBool(data []byte) (bool, error)
- func UnmarshalBytes(data []byte) ([]byte, error)
- func UnmarshalFloat(data []byte) (float64, error)
- func UnmarshalIndexedArray(data []byte) ([]interface{}, error)
- func UnmarshalInt(data []byte) (int64, error)
- func UnmarshalNil(data []byte) error
- func UnmarshalObject(data []byte, v reflect.Value) error
- func UnmarshalString(data []byte) (string, error)
- func UnmarshalUint(data []byte) (uint64, error)
- type MarshalOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodePHPString ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 UnmarshalBool ¶
func UnmarshalBytes ¶
func UnmarshalFloat ¶
func UnmarshalIndexedArray ¶
func UnmarshalInt ¶
func UnmarshalNil ¶
func UnmarshalString ¶
func UnmarshalUint ¶
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.