Magento

Magento 2.3.XX unable to serialize value and Failed to load the components errors on checkout

Written by Jigar Patel

Oct 28

Magento 2.3.XX unable to serialize value and Failed to load the components errors on checkout

CRITICAL Error: Unable to serialize value

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.”

Please follow below step:

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.

Written by Jigar Patel

Our Magento Store

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.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP