1
  1. Unable to Unserialize value error after upgrading Magento to 2.2.2

For this i have followed the solution from here: Magento 2.2: Unable to unserialize value? by @Sameer Bhayani and overrided the Json file of Magento\Framework\Serialize\Serializer\Json

But after that i was getting the below error:

Exception #0 (Exception): Warning: array_merge(): Argument #2 is not an array in /home/xxxxxxxxxxxxx/public_html/vendor/magento/module-catalog/Helper/Product/Configuration.php on line 118

Please Advise.

0

I faced the same issue and solved by below steps:

Step 1 : Change the following file /vendor/magento/framework/Serialize/Serializer/Json.php especially the unserialize function.

Here is the original unserialize function -:

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

Change the above function to the following -:

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

Also add the following is_serialized function in the same file /vendor/magento/framework/Serialize/Serializer/Json.php

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

Step 2: Use json_encode or better yet the method Magento\Framework\Serialize\Serializer\Json::serialize().

| improve this answer | |

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.