cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to serialize value.

Unable to serialize value.

I have this error on the main page of my website

1 exception(s):

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

 

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

#0 /chroot/home/publimer/publimercado.com.mx/html/vendor/magento/framework/App/PageCache/Kernel.php(153): Magento\Framework\Serialize\Serializer\Json->serialize(Array)

#1 /chroot/home/publimer/publimercado.com.mx/html/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 /chroot/home/publimer/publimercado.com.mx/html/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 /chroot/home/publimer/publimercado.com.mx/html/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 /chroot/home/publimer/publimercado.com.mx/html/generated/code/Magento/Framework/View/Result/Page/Interceptor.php(130): Magento\Framework\View\Result\Page\Interceptor->___callPlugins('renderResult', Array, NULL)

#5 /chroot/home/publimer/publimercado.com.mx/html/vendor/magento/framework/App/Http.php(139): Magento\Framework\View\Result\Page\Interceptor->renderResult(Object(Magento\Framework\App\Response\Http\Interceptor))

#6 /chroot/home/publimer/publimercado.com.mx/html/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch()

#7 /chroot/home/publimer/publimercado.com.mx/html/vendor/magento/framework/App/Bootstrap.php(257): Magento\Framework\App\Http\Interceptor->launch()

#8 /chroot/home/publimer/publimercado.com.mx/html/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))

#9 {main}

3 REPLIES 3

Re: Unable to serialize value.

@webgrupoga0b35 

Apply the below steps:

Go to /vendor/magento/framework/Serialize/Serializer/Json.php
Find the function unserialize($string)

 

Apply the below code to check if string is serialized then use serialize($string).

public function unserialize($string)
{
    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;
}

Add function to check if string is serialized:

function is_serialized($value, &$result = null)
{
    if (!is_string($value))
    {
        return false;
    }
    
    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':
            $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;
}

and clear the cache!

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Unable to serialize value.

Thanks! 

@Sanjay Jethva 

This is my Json.php file now, but i keep getting the same error

 

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Serialize\Serializer;

use Magento\Framework\Serialize\SerializerInterface;

/**
* Serialize data to JSON, unserialize JSON encoded data
*
* @api
* @since 100.2.0
*/
class Json implements SerializerInterface
{
/**
* {@inheritDoc}
* @since 100.2.0
*/
public function serialize($data)
{
$result = json_encode($data);
if (false === $result) {
throw new \InvalidArgumentException('Unable to serialize value.');
}
return $result;
}

/**
* {@inheritDoc}
* @since 100.2.0
*/
public function unserialize($string)
{
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;
}
function is_serialized($value, &$result = null)
{
if (!is_string($value))
{
return false;
}

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':
$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;
}
}

Re: Unable to serialize value.

i had the same issue...
for met the solution was adding a CRYPT_KEY=xxxxxxxxxxx to the .env file.

tried changing the /vendor/magento/framework/Serialize/Serializer/Json.php file but did not work for me.