Rust

serialize

Module

serialize, unserialize and var_export, in PHP 8.5's formats.

Every function here is checked against the real PHP function by bin/compat fuzz php-std (operations serialize.*).

PHPRust
serialize($value)serialize (Value)
unserialize($data, ['max_depth' => $n])unserialize, Options
var_export($value, true)var_export
warnings unserialize() emitsUnserialized::warnings, Error::Failed

Values are scalars, arrays and stdClass objects. Floats use serialize_precision = -1 (d:0.1;, d:1.0E+25;, d:INF;). An Value::Ext object is written as its Extension::to_array (PHP code serializes getArrayCopy(), not the object).

unserialize reads every format PHP writes for those values (N, b, i, d, s, S, a, O/C of stdClass, and the r/R back-references), with PHP's error offsets, warnings and depth limit. Back-references resolve to the value at that slot when they are read, which is what PHP produces for data serialize writes; Value has no references or object identity, so they become copies. Objects of other classes, enums (E:), and back-references to a container that is still being read (recursive structures) cannot be represented and fail with Error::Unsupported.

Contents

use php_std::serialize::Options;
Rust
pub struct Options

unserialize() options.

Also implements Default.
use php_std::serialize::Unserialized;
Rust
pub struct Unserialized

A successful unserialize(): the value and the warnings PHP emitted (for example unserialize(): Extra data starting at offset 4 of 5 bytes).

use php_std::serialize::Error;
Rust
pub enum Error

Why unserialize() failed.

Also implements Display.
use php_std::serialize::serialize;
Rust
pub fn serialize<X: Extension>(value: &Value<X>) -> Vec<u8>

serialize($value): PHP's byte string.

use php_std::serialize::serialize_into;
Rust
pub fn serialize_into<X: Extension>(out: &mut Vec<u8>, value: &Value<X>)

serialize($value), appended to out.

use php_std::serialize::unserialize;
Rust
pub fn unserialize(data: &[u8], options: &Options) -> Result<Unserialized, Error>

unserialize($data, $options).

use php_std::serialize::var_export;
Rust
pub fn var_export<X: Extension>(value: &Value<X>) -> Vec<u8>

var_export($value, true): PHP source code for the value, as bytes (string contents are copied verbatim).