Magento – Unable to serialize value problem with Magento 2.2.6

magento2magento2.2magento2.2.6

Please teach in order to solve the problem.

cd /vagrant
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy
sudo php bin/magento setup:static-content:deploy ja_JP
sudo php bin/magento cache:flush;
sudo php bin/magento cache:clean
sudo php bin/magento indexer:reindex
sudo find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
sudo find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
sudo chown -R :www-data .
sudo chmod u+x bin/magento

1 exception(s):
Exception #0 (InvalidArgumentException): Unable to serialize value.

Exception #0 (InvalidArgumentException): Unable to serialize value.
#0 /vagrant/vendor/magento/framework/App/PageCache/Kernel.php(153):

Magento\Framework\Serialize\Serializer\Json->serialize(Array)
#1 /vagrant/vendor/magento/module-page-cache/Model/Controller/Result/BuiltinPlugin.php(96):
Magento\Framework\App\PageCache\Kernel->process(Object(Magento\Framework\App\Response\Http\Interceptor))
#2 /vagrant/vendor/magento/framework/Interception/Interceptor.php(146):
Magento\PageCache\Model\Controller\Result\BuiltinPlugin->afterRenderResult(Object(Magento\Framework\View\Result\Page\Interceptor),
Object(Magento\Framework\View\Result\Page\Interceptor),
Object(Magento\Framework\App\Response\Http\Interceptor))
#3 /vagrant/vendor/magento/framework/Interception/Interceptor.php(153):
Magento\Framework\View\Result\Page\Interceptor->Magento\Framework\Interception{closure}(Object(Magento\Framework\App\Response\Http\Interceptor))
#4 /vagrant/generated/code/Magento/Framework/View/Result/Page/Interceptor.php(26):
Magento\Framework\View\Result\Page\Interceptor->___callPlugins('renderResult',
Array, Array)
#5 /vagrant/vendor/magento/framework/App/Http.php(139): Magento\Framework\View\Result\Page\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))
#6 /vagrant/vendor/magento/framework/App/Bootstrap.php(257): Magento\Framework\App\Http->launch()
#7 /vagrant/pub/index.php(37): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#8 {main}

Best Answer

The problem is in /vendor/magento/framework/Serialize/Serializer/Json.php there is a function unserialize($string)

There is a workaround - you can check if string is serialized and then use serialize($string). Change unserialize to:

public function unserialize($string)
{
    /* Workaround: serialize first if is serialized */
    if($this->is_serialized($string))
    {
        $string = $this->serialize($string);
    }
    $result = json_decode($string, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
         throw new \InvalidArgumentException('Unable to unserialize value.');

    }
    return $result;
}

and add function to check if string is serialized:

function is_serialized($value, &$result = null)
{
    // Bit of a give away this one
    if (!is_string($value))
    {
        return false;
    }
    // Serialized false, return true. unserialize() returns false on an
    // invalid string or it could return false if the string is serialized
    // false, eliminate that possibility.
    if ($value === 'b:0;')
    {
        $result = false;
        return true;
    }
    $length = strlen($value);
    $end    = '';
    switch ($value[0])
    {
        case 's':
            if ($value[$length - 2] !== '"')
            {
                return false;
            }
        case 'b':
        case 'i':
        case 'd':
            // This looks odd but it is quicker than isset()ing
            $end .= ';';
        case 'a':
        case 'O':
            $end .= '}';
            if ($value[1] !== ':')
            {
                return false;
            }
            switch ($value[2])
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    break;
                default:
                    return false;
            }
        case 'N':
            $end .= ';';
            if ($value[$length - 1] !== $end[0])
            {
                return false;
            }
            break;
        default:
            return false;
    }
    if (($result = @unserialize($value)) === false)
    {
        $result = null;
        return false;
    }
    return true;
}
Related Topic