Edit in GitHubLog an issue

Serialize library

This library provides a secure way of serializing and unserializing strings, integers, floats, booleans, and arrays.

Magento's Serialize library provides the Magento\Framework\Serialize\SerializerInterface and the Json and Serialize implementations for serializing data.

Serialization#

The main purpose of data serialization is to convert data into a string using serialize() to store in a database, a cache, or pass onto another layer in the application.

The other half of this process uses the unserialize() function to reverse the process and convert a serialized string back into string, integer, float, boolean, or array data.

Implementations#

Json (default)#

The Magento\Framework\Serialize\Serializer\Json class serializes and unserializes data using the JSON format.

JsonHexTag#

The Magento\Framework\Serialize\Serializer\JsonHexTag class serializes and unserializes data using the JSON format using the JSON_HEX_TAG option enabled.

Base64Json#

The Magento\Framework\Serialize\Serializer\Base64Json class serializes and encodes in the base64 format, and decodes the base64 encoded string and unserializes data using the JSON format.

Serialize#

The Magento\Framework\Serialize\Serializer\Serialize class is less secure than the Json implementation but provides better performance on large arrays.

FormData#

The Magento\Framework\Serialize\Serializer\FormData class unserializes the form data using the JSON format. This class does not serialize objects to a form data format.

Usage#

Declare SerializerInterface as a constructor dependency to get an instance of a serializer class.

Copied to your clipboard
1use Magento\Framework\Serialize\SerializerInterface;
2
3...
4
5/**
6 * @var SerializerInterface
7 */
8private $serializer;
9
10...
11
12public function __construct(SerializerInterface $serializer) {
13 $this->serializer = $serializer;
14}

\ The following example shows how to use a serializer's serialize() and unserialize() functions to store and retrieve array data from a cache:

Copied to your clipboard
1
2...
3
4/**
5 * @var string
6 */
7private $cacheId = 'mySerializedData';
8
9...
10
11/**
12 * Save data to cache
13 * @param array $data
14 *
15 * @return bool
16 */
17public function saveDataToCache($data)
18{
19 return $this->getCache()->save($this->serializer->serialize($data), $this->cacheId);
20}
21
22...
23
24/**
25 * Load data from cache
26 *
27 * @return array
28 */
29public function loadDataFromCache()
30{
31 $data = $this->getCache()->load($this->cacheId);
32 if (false !== $data) {
33 $data = $this->serializer->unserialize($data);
34 }
35 return $data;
36}
37...

Backward compatibility note#

The SerializerInterface interface and its implementations only exist since Adobe Commerce and Magento Open Source version 2.2. Because of this, it is not possible to use these classes in code that has to be compatible with Adobe Commerce and Magento Open Source 2.1 or 2.0.

In code that is compatible with earlier versions of Adobe Commerce and Magento Open Source, constructor dependency injection can not be used to get an instance of SerializerInterface. Instead, a runtime check if the SerializerInterface definition exists can made, and if it does, it can be instantiated by directly accessing the object manager using a static method. Alternatively, a check against the Adobe Commerce and Magento Open Source version or the magento/framework Composer package version works too. If the interface does not exist or an earlier version of Adobe Commerce and Magento Open Source is being executed, the appropriate native PHP serialization function has to be called, e.g. \serialize() or \json_encode(), depending on the usercase.

Here is an example:

Copied to your clipboard
1use Magento\Framework\Serialize\SerializerInterface;
2use Magento\Framework\App\ObjectManager;
3
4...
5/**
6 * @param mixed $data
7 * @return string
8 */
9private function serialize($data)
10{
11 if (class_exists(SerializerInterface::class)) {
12 $objectManager = ObjectManager::getInstance();
13 $serializer = $objectManager->create(SerializerInterface::class);
14 return $serializer->serialize($data);
15 }
16 return \serialize($data);
17}
18...
19
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.