Rust

serialize

Module

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

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 utopia_php_std::serialize::Options;
Rust
pub struct Options {
    pub max_depth: i64,
}

unserialize() options.

Also implements Default.
use utopia_php_std::serialize::Unserialized;
Rust
pub struct Unserialized {
    pub value: Value,
    pub warnings: Vec<String>,
}

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

use utopia_php_std::serialize::Error;
Rust
pub enum Error {
    Failed {
        offset: Option<usize>,
        warnings: Vec<String>,
    },
    Value(String),
    Unsupported(&'static str),
}

Why unserialize() failed.

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

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

use utopia_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 utopia_php_std::serialize::unserialize;
Rust
pub fn unserialize(data: &[u8], options: &Options) -> Result<Unserialized, Error>

unserialize($data, $options).

use utopia_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).