In this post I am going to explain How to Unable to serialize value.
Error : After Magento 2 version upgrade error on check page is as below.
“report.CRITICAL: Unable to serialize value. Error: Malformed UTF-8 characters, possible incorrectly encoded.”
Please follow the steps to resolve this issue.
1. Create registration.php file at below Path
Path: app/code/<vendor>/<yourmodule>
1 2 3 4 5 6 | <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, '<vendor>_<yourmodule>', __DIR__ ); |
2. Create module.xml file at below path
Path: app/code/<vendor>/<yourmodule>/etc
1 2 3 4 5 6 7 8 | <?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>_<yourmodule>" setup_version="1.0.0"> <sequence> <module name="Magento_Framework"/> </sequence> </module> </config> |
3. Create di.xml file at below path
Path: app/code/<vendor>/<yourmodule>/etc
1 2 3 4 5 | <?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Framework\Serialize\Serializer\Json" type="<vendor>\<yourmodule>\Rewrite\Magento\Framework\Serialize\Serializer\Json"/> <preference for="Magento\Framework\Serialize\Serializer\JsonHexTag" type="<vendor>\<yourmodule>\Rewrite\Magento\Framework\Serialize\Serializer\JsonHexTag"/> </config> |
4. Create Json.php file at below path
Path: app/code/<vendor>/<yourmodule>/Rewrite/Magento/Framework/Serialize/Serializer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php namespace <vendor>\<yourmodule>\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; } } |
5. Create JsonHexTag.php file at below path
Path: app/code/<vendor>/<yourmodule>/Rewrite/Magento/Framework/Serialize/Serializer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php namespace <vendor>\<yourmodule>\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; } } |
That’s it.
Note: In order to install this extension, after adding files you need to run upgrade and deploy commands.
If you have any query about the above code then you can ask in the comment section below.
Check this Best Magento 2 Tutorial.
Thank You.
Leave a Reply