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

Written by Jigar Patel

Oct 28, 2020

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.

Jigar Patel

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP