[Solved] “Unable to Unserialize Value” in Magento 2.2

[Solved] Unable to Unserialize Value in Magento 2.2

Have you ever encountered an issue labeled “unable to unserialize value” when navigating through the system configuration in Magento 2.2’s admin panel? This particular hiccup can be attributed to the \Magento\Framework\Serialize\Serializer\Json class.

The culprit lies within the file path: vendor/magento/framework/Serialize/Serializer/Json.php. The problem stems from the unserialize($string) function, which throws an exception if the string is already serialized.

Solution:-

Open file: vendor/magento/framework/Serialize/Serializer/Json.php

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

Modify this function:

    public function unserialize($string)
    {
        /* Added code START */
        if($this->is_serialized($string)){
           $string = $this->serialize($string);
        }
        /* Added code END */

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

Also add is_serialized() function in the same file vendor/magento/framework/Serialize/Serializer/Json.php at the end:

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

After making the necessary updates to the JSON file, we successfully saved the configuration without encountering any issues. Subsequently, we restored the settings to their original state, and from that point on, everything functioned as anticipated.

Thank you for reading my Article! Feel free to share your thoughts or ask any questions in the comments section below and spread the word by sharing. Your engagement is appreciated!

Leave a reply

Your email address will not be published. Required fields are marked *

Cookies Notice

Our website use cookies. If you continue to use this site we will assume that you are happy with this.