When I upgraded the Magento on the checkout page and that was “report.CRITICAL: Unable to serialize value. Error: Malformed UTF-8 characters, possible incorrectly encoded.”
1) Create registration.php file at Path :- app/code/Dolphin/Serializer
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Dolphin_Serializer', __DIR__ );
2) Create module.xml file at Path :- app/code/Dolphin/Serializer/etc
<?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="Dolphin_Serializer" setup_version="1.0.0"> <sequence> <module name="Magento_Framework"/> </sequence> </module> </config>
3) Create di.xml file at path :- app/code/Dolphin/Serializer/etc
<?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="Dolphin\Serializer\Rewrite\Magento\Framework\Serialize\Serializer\Json"/> <preference for="Magento\Framework\Serialize\Serializer\JsonHexTag" type="Dolphin\Serializer\Rewrite\Magento\Framework\Serialize\Serializer\JsonHexTag"/> </config>
4) Create Json.php file at path :- app/code/Dolphin/Serializer/Rewrite/Magento/Framework/Serialize/Serializer
<?php namespace Dolphin\Serializer\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 path :- app/code/Dolphin/Serializer/Rewrite/Magento/Framework/Serialize/Serializer
<?php namespace Dolphin\Serializer\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; } }
Hope this helps. Thanks.
In Magento
Apr 02, 2023
Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.