Core\Serialize::encode
Core\Serialize::encode(mixed $value): bytesCore\Serialize::encode( mixed $value, ): bytes
Core\Serialize::encode() turns a value into bytes, and Core\Serialize::decode() builds the same value again from them.
The value can be a number, a string, an array or an object. Arrays and objects can contain other arrays and objects. If two places point to the same object, they still point to one object after decode(). You can save the bytes in a file or a cache, or keep them as a copy of a value at one moment.
Some values cannot be turned into bytes: a callable, an open file or connection, and an object with a secret property. For these values, encode() throws a LogicError. Some Core objects cannot be turned into bytes either. The message of the LogicError names their class.
Good to know: the bytes use a format that only Novis reads.
The examples below show a list turned into bytes and back, two orders that share one customer, and a copy of a shopping cart that undoes a change.
Parameters
$valuemixedThe value to turn into bytes. Two places that point to the same object still point to one object afterdecode().
Return value
Returns bytes. The bytes. They are tainted when $value can contain text from outside the program. That is a tainted string, or a mixed or object value, anywhere inside it. In that case, call Core\Taint::assertTrustedBytes on the bytes before decode().
Errors
LogicError$valuecontains a callable, an open file or connection, or an object with asecretproperty. It is also thrown when arrays and objects are nested more than 256 levels deep. SomeCoreobjects cannot be written either, and the message names the class.
Examples
Turn a list into bytes
<?nvs// Shows how to turn a list of names into bytes and read the list back.
array<string> $names = ['Anna', 'Ben', 'Clara'];
// `encode()` returns the whole list as one `bytes` value.bytes $payload = Core\Serialize::encode($names);echo 'The list is ', Core\Bytes::length($payload), " bytes long.\n";
// `decode()` returns the list again. This prints the three names.array<tainted string> $back = Core\Serialize::decode($payload) as array<string>;foreach ($back as tainted string $name) { echo $name, "\n";}The list is 52 bytes long.AnnaBenClaraTwo orders share one customer
<?nvs// Shows that two objects which point to the same object still share it after `decode()`.
class Customer{ public string $name; public function constructor(string $name) { $this->name = $name; }}
class Order{ public uint $number; public Customer $customer; public function constructor(uint $number, Customer $customer) { $this->number = $number; $this->customer = $customer; }}
class OrderBook{ public array<Order> $orders = [];}
Customer $anna = new Customer('Anna');OrderBook $book = new OrderBook();$book->orders[] = new Order(1001, $anna);$book->orders[] = new Order(1002, $anna);
OrderBook $copy = Core\Serialize::decode(Core\Serialize::encode($book)) as OrderBook;
// Both orders in the copy point to one customer. A change through the first// order is visible through the second. This prints "Anna Schmidt".$copy->orders[0]->customer->name = 'Anna Schmidt';echo $copy->orders[1]->customer->name, "\n";
// The original orders are not changed. This prints "Anna".echo $book->orders[1]->customer->name, "\n";Anna SchmidtAnnaUndo a change to a cart
<?nvs// Shows how to keep a copy of a shopping cart, so a change can be undone.
class Cart{ public array<uint> $items = [];
public function total(): uint { uint $sum = 0; foreach ($this->items as uint $count) { $sum = $sum + $count; } return $sum; }}
Cart $cart = new Cart();$cart->items['apple'] = 3;$cart->items['bread'] = 1;
// The bytes are a copy of the cart at this moment.bytes $saved = Core\Serialize::encode($cart);
// The customer adds ten more apples. This prints 14.$cart->items['apple'] = 13;echo $cart->total(), "\n";
// The customer undoes the change. `decode()` returns the cart as it was, so this prints 4.$cart = Core\Serialize::decode($saved) as Cart;echo $cart->total(), "\n";144