Core\Serialize::decode
Core\Serialize::decode(bytes $payload): mixedCore\Serialize::decode( bytes $payload, ): mixed
Core\Serialize::decode() builds a value again from the bytes that Core\Serialize::encode() returned.
An object in the result is a new object of the class with the same name in your program. Its constructor does not run. The bytes must come from encode(). If they are cut short, have extra bytes at the end, or name a class your program does not have, decode() throws a ParseError. decode() also throws a ParseError for bytes that name a generator, a function value or an open resource, such as a file. It also throws a ParseError when an object in the bytes has a value of the wrong type, such as a string where the class has an int.
Bytes that came from outside the program, such as a request body or a cookie, are tainted. A call to decode() with tainted bytes does not compile. This stops a visitor from sending bytes that build any object they choose.
The examples below show an object read back without its constructor, bytes that are cut short, and a list of jobs saved to a file and loaded again.
Parameters
$payloadbytesThe bytes thatencode()returned. If they aretaintedand your program wrote them itself, callCore\Taint::assertTrustedByteson them first.
Return value
Returns mixed. The value. An object in it is a new object of the class with the same name in this program, and its constructor does not run.
Errors
ParseError$payloadwas not written byencode(), is incomplete, or has extra bytes at the end. It is also thrown when it names a class this program does not have, or a class whose properties are different now. Bytes that name a generator, a function value or an open resource, such as a file, are not read either. Bytes with a value of the wrong type in an object are not read, such as a string where the class has anint.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Serialize::decode gives a revocable object that is in no collection. Encoded data cannot change which collections contain a revocable object. Data that tries throws a ParseError. |
Examples
Read back an object
<?nvs// Shows how to read an object back from bytes. Its constructor does not run a second time.
class Settings{ public string $theme; public uint $pageSize; public function constructor(string $theme, uint $pageSize) { echo "The constructor runs.\n"; $this->theme = $theme; $this->pageSize = $pageSize; }}
// This prints "The constructor runs." once.bytes $payload = Core\Serialize::encode(new Settings('dark', 50));
// `decode()` returns `mixed`. `as Settings` checks that the result is a `Settings` object.Settings $settings = Core\Serialize::decode($payload) as Settings;
// This prints "dark 50".echo $settings->theme, ' ', $settings->pageSize, "\n";The constructor runs.dark 50Bytes that are cut short
<?nvs// Shows what happens when the bytes are not complete, for example after a failed download.
bytes $payload = Core\Serialize::encode(['Anna', 'Ben', 'Clara']);
// These are only the first 10 bytes of the list.bytes $cut = Core\Bytes::slice($payload, 0, 10);
// `decode()` throws a `ParseError`. This prints the error message.try { Core\Serialize::decode($cut); echo "The list was read.\n";} catch (ParseError $e) { echo $e->message, "\n";}Core\Serialize::decode(): the payload ends in the middle of a valueLoad saved jobs from a file
<?nvs// Shows how a command-line tool saves its list of jobs to a file and loads it on the next run.
class Job{ public string $name; public uint $tries; public function constructor(string $name, uint $tries) { $this->name = $name; $this->tries = $tries; }}
class JobList{ public array<Job> $jobs = [];}
JobList $list = new JobList();$list->jobs[] = new Job('send-invoices', 0);$list->jobs[] = new Job('resize-images', 2);
// `Core\Serialize::encode` gives `bytes`. `Core\IO::write` saves them to the file as they are.string $path = Core\Path::join(Core\IO::createTemporaryDirectory(), 'jobs.bin');Core\IO::write($path, Core\Serialize::encode($list));
// On the next run, the tool reads the bytes back and builds the list again.bytes $saved = Core\IO::readBytes($path);JobList $loaded = Core\Serialize::decode($saved) as JobList;
// This prints each job and how many times it was tried.foreach ($loaded->jobs as Job $job) { echo $job->name, ': ', $job->tries, "\n";}send-invoices: 0resize-images: 2