6

How can this be solved after upgrading magento 2.2.X to 2.3.X : enter image description here

6
  • Try refreshing Redis Cache. Aug 27, 2019 at 7:09
  • yes i have done this but it didn't work even Aug 27, 2019 at 7:10
  • After running which command you are getting this error
    – ASQ
    Aug 27, 2019 at 7:12
  • after upgrading magento 2.2.X to 2.3.X and i have deployed all magento command. Aug 27, 2019 at 7:14
  • try to run rm -rf pub/static/frontend and then check and let me know
    – ASQ
    Aug 27, 2019 at 7:23

3 Answers 3

20

Go to this file location and paste below code and best way try to override this file : vendor/magento/framework/Serialize/Serializer/Json.php

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;
}

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;
}

How to override this file

The preference for the SerializerInterface was added since 2.2.x version of the Magento, and declared in the app/etc/di.xml:

<preference for="Magento\Framework\Serialize\Json" type="Magento\Framework\Serialize\Serializer\Json" />

Override Hint :

Then create the class [Vendor][Module]\Serialize\Serializer\Json that extends Magento\Framework\Serialize\Serializer\Json and make your changes in there.

1
  • This solved my issue also on Arabic site.
    – ishaq
    Dec 25, 2020 at 11:15
5

The below code is working for me after upgrade 2.3.0 to 2.4.2 it will be help

Create di.xml in app/etc/code/Vendor/Module/etc/di.xml

<?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\Module\Serialize\Serializer\Json" />
</config>

Create Json.php file in app/etc/code/Vendor/Module/Serialize/Serializer/Json.php

<?php
namespace Vendor\Module\Serialize\Serializer;

class Json extends \Magento\Framework\Serialize\Serializer\Json
{
    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;
    }

    public function unserialize($string)
    {
        $result = json_decode($string, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            if(false !== @unserialize($string)){
                return unserialize($string);
            }
            throw new \InvalidArgumentException('Unable to unserialize value.');
        }
        return $result;
    }

    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;
    }
}
1

Magento 2 checkout cart showing unable to unserialize value. Error syntax error for that error this solution may work.

Go to this path and replace method with below code. After applying this code please run below command

redis-cli flushall

vendor/magento/framework/Serialize/Serializer/Json.php

public function unserialize($string)
    {
        $result = json_decode($string, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            if(false !== @unserialize($string)){
                return unserialize($string);
            }
            throw new \InvalidArgumentException('Unable to unserialize value.');
        }
        return $result;
    }

And try to override this file using preference :

<preference for="Magento\Framework\Serialize\Serializer\Json" type="Namespace\ModuleName\Serialize\Serializer\Json" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.