Hello Magento Friends,
How are you all doing? I am here with this tutorial to solve the issue of unable to serialize value in Magento 2.
While upgrading the Magento 2 version, sometimes you may face the unable to serialize value error on the checkout page.
“report.CRITICAL: Unable to serialize value. Error: Malformed UTF-8 characters, possible incorrectly encoded.“
Upgrade your Magento store error-free with the Best Magento Upgrade service
For now, let’s move on to solve the unable to serialize value error in Magento 2.
How to Solve Unable To Serialize Value Error in Magento 2?
Step 1: Create registration.php at the given below path
app\code\Vendor\Extension\registration.php
Now, add the code as follows
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_Extension', __DIR__ );
Step 2: Create module.xml at the below path
app\code\Vendor\Extension\etc\module.xml
Then add the code as mentioned below
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Extension" setup_version="1.0.0"> <sequence> <module name="Magento_Framework" /> </sequence> </module> </config>
Step 3: Create di.xml at the following path
app\code\Vendor\Extension\etc\di.xml
Then append the code as given below
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <preference for="Magento\Framework\Serialize\Serializer\Json" type="Vendor\Extension\Rewrite\Magento\Framework\Serialize\Serializer\Json" /> <preference for="Magento\Framework\Serialize\Serializer\JsonHexTag" type="Vendor\Extension\Rewrite\Magento\Framework\Serialize\Serializer\JsonHexTag" /> </config>
Step 4: Now, create Json.php at the path mentioned below
app\code\Vendor\Extension\Rewrite\Magento\Framework\Serialize\Serializer\Json.php
And include the code in it as given below
<?php namespace Vendor\Extension\Rewrite\Magento\Framework\Serialize\Serializer; class Json extends \Magento\Framework\Serialize\Serializer\Json { public function utf8ize($mixed) { if (is_array($mixed)) { foreach ($mixed as $key => $value) { $mixed[$key] = $this->utf8ize($value); } } elseif (is_string($mixed)) { return mb_convert_encoding($mixed, "UTF-8", "UTF-8"); } return $mixed; } /** * @inheritDoc * @since 101.0.0 */ public function serialize($data) { $result = json_encode($this->utf8ize($data)); if (false === $result) { throw new \InvalidArgumentException("Unable to serialize value. Error: " . json_last_error_msg()); } return $result; } /** * @inheritDoc * @since 101.0.0 */ public function unserialize($string) { $result = json_decode($string, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new \InvalidArgumentException("Unable to unserialize value. Error: " . json_last_error_msg()); } return $result; } }
Step 5: Now, create JsonHexTag.php at the following path
app\code\Vendor\Extension\Rewrite\Magento\Framework\Serialize\Serializer\JsonHexTag.php
Then write the code as below
<?php namespace Vendor\Extension\Rewrite\Magento\Framework\Serialize\Serializer; class JsonHexTag extends \Magento\Framework\Serialize\Serializer\JsonHexTag { /** * @inheritDoc * @since 102.0.1 */ public function serialize($data): string { $data = $this->utf8convert($data); $result = json_encode($data, JSON_HEX_TAG); if (false === $result) { throw new \InvalidArgumentException('Unable to serialize value.'); } return $result; } public function utf8convert($mixed, $key = null) { if (is_array($mixed)) { foreach ($mixed as $key => $value) { $mixed[$key] = $this->utf8convert($value, $key); } } elseif (is_string($mixed)) { $fixed = mb_convert_encoding($mixed, "UTF-8", "UTF-8"); return $fixed; } return $mixed; } }
Conclusion:
This way you can easily get rid of the serialize value error in Magento 2. If you have any doubts about the above steps, contact me via the comment section. Share the solution with your other developer friends to help them get out of the unable to serialize error.
Happy Coding!