serialize
Moduleserialize, 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.*).
| PHP | Rust |
|---|---|
serialize($value) | serialize (Value) |
unserialize($data, ['max_depth' => $n]) | unserialize, Options |
var_export($value, true) | var_export |
warnings unserialize() emits | Unserialized::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;pub struct Optionsunserialize() options.
Default.use php_std::serialize::Unserialized;pub struct UnserializedA 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;pub enum ErrorWhy unserialize() failed.
Display.use php_std::serialize::serialize;pub fn serialize<X: Extension>(value: &Value<X>) -> Vec<u8>serialize($value): PHP's byte string.
use php_std::serialize::serialize_into;pub fn serialize_into<X: Extension>(out: &mut Vec<u8>, value: &Value<X>)serialize($value), appended to out.
use php_std::serialize::unserialize;pub fn unserialize(data: &[u8], options: &Options) -> Result<Unserialized, Error>unserialize($data, $options).
use php_std::serialize::var_export;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).