cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to unserialize value - Error

Unable to unserialize value - Error

Hi all.

I know there is quite a bit on the web regarding the issue with the error "main.CRITICAL: Unable to unserialize value. Error: Syntax error [] []".

I'm getting this: "Unable to unserialize value. Error: Syntax error".

I used the solution to update the Serializer/Json.php file but all that has changed is the error message is now: "main.CRITICAL: Unable to unserialize value. [] []"

This hasn't solved my issue.

Update code i used is:

<?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 101.0.0
 */
class Json implements SerializerInterface
{
    /**
     * @inheritDoc
     * @since 101.0.0
     */
    public function serialize($data)
    {
        $result = json_encode($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)
{
    /* Added the following if clause to resolve the issue */
    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)
{
    // 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;
}
}

Any other suggestions please?

Thank you

Andy

4 REPLIES 4

Re: Unable to unserialize value - Error

@Andy_Acute 

 

Can you try below shared article once:

https://meetanshi.com/blog/solved-unable-to-unserialize-value-in-magento-2-2/

 

Manish Mittal
https://www.manishmittal.com/

Re: Unable to unserialize value - Error

Hi @Andy_Acute ,

 

 if you used redis make sure check redis configuration your env.php in app/etc
You can re-config redis with commands below. This is recommended from magento

bin/magento setup:config:set --session-save=redis --session-save-redis-host=redis --cache-backend-redis-db=0
bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=redis --cache-backend-redis-db=1

try clear cache and flush redis

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

Re: Unable to unserialize value - Error

Hi Manish

This is the code I have in my Json.php file!!

Please look at my OP

Andy

 

Re: Unable to unserialize value - Error

@Andy_Acute 

 

It seems okay as suggested solution, still are you facing same?

Manish Mittal
https://www.manishmittal.com/