12

I have upgraded website from 2.1.6 to 2.2.1 and facing Unable to serialize value error in frontend and backend.

{"0":"Unable to serialize value.","1":"#0 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/Translate.php(494): Magento\\Framework\\Serialize\\Serializer\\Json->serialize(Array)\n
#1 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/Translate.php(190): Magento\\Framework\\Translate->_saveCache()\n
#2 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/App\/Area.php(244): Magento\\Framework\\Translate->loadData(NULL, false)\n
#3 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/App\/Area.php(215): Magento\\Framework\\App\\Area->_initTranslate()\n
#4 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/App\/Area.php(142): Magento\\Framework\\App\\Area->_loadPart('translate')\n
#5 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/View\/DesignLoader.php(55): Magento\\Framework\\App\\Area->load('translate')\n
#6 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/App\/Action\/Plugin\/Design.php(48): Magento\\Framework\\View\\DesignLoader->load()\n
#7 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/Interception\/Interceptor.php(121): Magento\\Framework\\App\\Action\\Plugin\\Design->beforeDispatch(Object(Magento\\Cms\\Controller\\Index\\Index\\Interceptor), Object(Magento\\Framework\\App\\Request\\Http))\n
#8 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/Interception\/Interceptor.php(153): Magento\\Cms\\Controller\\Index\\Index\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n
#9 \/var\/www\/vhosts\/demo.com\/eiselec\/generated\/code\/Magento\/Cms\/Controller\/Index\/Index\/Interceptor.php(39): Magento\\Cms\\Controller\\Index\\Index\\Interceptor->___callPlugins('dispatch', Array, Array)\n
#10 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/App\/FrontController.php(55): Magento\\Cms\\Controller\\Index\\Index\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n
#11 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Magento\\Framework\\App\\FrontController->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n
#12 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Magento\\Framework\\App\\FrontController\\Interceptor->___callParent('dispatch', Array)\n
#13 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/module-store\/App\/FrontController\/Plugin\/RequestPreprocessor.php(94): Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n
#14 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/Interception\/Interceptor.php(135): Magento\\Store\\App\\FrontController\\Plugin\\RequestPreprocessor->aroundDispatch(Object(Magento\\Framework\\App\\FrontController\\Interceptor), Object(Closure), Object(Magento\\Framework\\App\\Request\\Http))\n
#15 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/module-page-cache\/Model\/App\/FrontController\/BuiltinPlugin.php(73): Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n
#16 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/Interception\/Interceptor.php(135): Magento\\PageCache\\Model\\App\\FrontController\\BuiltinPlugin->aroundDispatch(Object(Magento\\Framework\\App\\FrontController\\Interceptor), Object(Closure), Object(Magento\\Framework\\App\\Request\\Http))\n
#17 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/Interception\/Interceptor.php(153): Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n
#18 \/var\/www\/vhosts\/demo.com\/eiselec\/generated\/code\/Magento\/Framework\/App\/FrontController\/Interceptor.php(26): Magento\\Framework\\App\\FrontController\\Interceptor->___callPlugins('dispatch', Array, NULL)\n
#19 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/App\/Http.php(135): Magento\\Framework\\App\\FrontController\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n
#20 \/var\/www\/vhosts\/demo.com\/eiselec\/vendor\/magento\/framework\/App\/Bootstrap.php(256): Magento\\Framework\\App\\Http->launch()\n
#21 \/var\/www\/vhosts\/demo.com\/eiselec\/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http))\n
#22 {main}","url":"\/","script_name":"\/index.php"}

Let me know how can i resolve it.

Thanks

  • Hi, I am talking about serialize value not unseriaize value. – Meetanshi Nov 21 '17 at 6:19
  • Have you tried clearing the cache ? not only Magento cache, external caches too if any . – MGento Nov 21 '17 at 6:22
  • yes, i have tried. – Meetanshi Nov 21 '17 at 6:23
  • Can you try to find out, what data you are trying to serialize ? Try traversing through your third party modules and find out, from which module, this error is triggered. You may probably need to override the serialize function, in the file /vendor/magento/framework/Serialize/Serializer/Json.php – MGento Nov 21 '17 at 8:51
  • Is this error happening during the DB update or after you've updated the DB to 2.2.1? – drew7721 Nov 24 '17 at 19:48
4

i have the same behaviour with an template. I copied the code for the error to my serializer to get my problem.

As soon as i switch to de_DE and regenerate my static code via

sudo php bin/magento setup:static-content:deploy de_DE --jobs=0 -f

it throws "Malformed UTF-8 characters, possibly incorrectly encoded".

So i looked up the files i changed in the template folder (i.e. code/Mytheme/Bannerslider/i18n/de_DE.csv) and downloaded them via WinSCP. Notepad++ showed "Ansii Encoding" - hard it i used "magento i18n:collect-phrases" for the translation file to create.

magento2dev # encguess app/code/MyTheme/Bannerslider/i18n/de_DE.csv

app/code/MyTheme/Bannerslider/i18n/de_DE.csv US-ASCII

magento2dev # locale
LANG=de_DE.UTF-8
......

So i changed the files manually in Notepad++, uploaded them, deployed the static content and reset all permissions - en voila it works.

So, the bug could be in your i18n csv file.

1

Be careful with substr function. It does not support UTF-8. And this can break the FPC. Use mb_substr

0

For me the solution was to replace all special characters like "ä" in the translation csv file with html versions of the same character like this:

ä

Then I cleared caches and reloaded frontend.

1

In my case the cause to a UTF8 Encoding problem, was non-multibyte safe shortening of product names:

$productName = strlen($productName) > 60 ? substr($productName,0,60)."..." : 
      $productName;

So a

012345678901234567890123456789012345678901234567890123456 Außengewinde 

became

012345678901234567890123456789012345678901234567890123456 Au�...
  • This was also the issue for our. ixed it by replacing "substr" with "mb_substr" – amesh Jul 30 at 7:15
1

I ended up to exactly to the same situation. After adding the code above I got "Malformed UTF-8 characters, possibly incorrectly encoded"

I suppose that you are not using the default language. Try to change language to "default" en_US.

Meetanshi - Which language you are using in front-end and is the static-content creation also failing?

  • Hi @A.P, I am facing the same error and i am using de_DE language. – Meetanshi Dec 3 '17 at 10:28
  • Try change to en_US. Table core_config_data (general/locale/code) to en_US – A.P Dec 3 '17 at 10:45
  • same error after changing to en_US. – Meetanshi Dec 3 '17 at 10:54
  • I managed to get it up, but dead-end when trying get back to fi_FI. Did you cleared the cache? – A.P Dec 3 '17 at 11:02
  • yes, i cleared cache – Meetanshi Dec 3 '17 at 11:03
1

I've ran in the same issue with the upgrade to 2.2.1. I found this articel to be very helpful http://devdocs.magento.com/guides/v2.2/ext-best-practices/tutorials/serialized-to-json-data-upgrade.html

Data stored in the DB should not be serialized anymore, it should now be saved as a JSON object.

Most modules make a data update that unserializes the data in the DB and stores it again under a JSON format. (BTW It took quite a while to run this...)

Hence, if one of your modules saves data that is serialized in the DB that data might not be readable by Magento anymore, you will need to make a data upgrade Setup file. Also, it might be a 3rd party module that needs to be updated to a 2.2+ compatible version.

If you do serialize on unserialize data anywhere in your code you might have to change that as well.

I hope this gives you a better insight at what's causing this error.

Cheers!

  • Make sure to read the Release Notes for 2.2.1, lots has changed, including the path for the generation folder. ;) – drew7721 Nov 24 '17 at 19:58
10
+25

As I can see this error comes from the method:

/**
 * Saving data cache
 *
 * @return $this
 */
protected function _saveCache()
{
    $this->_cache->save($this->getSerializer()->serialize($this->getData()), $this->getCacheId(true), [], false);
    return $this;
}

and serializer which is not found comes from the method:

/**
 * Get serializer
 *
 * @return \Magento\Framework\Serialize\SerializerInterface
 * @deprecated 100.2.0
 */
private function getSerializer()
{
    if ($this->serializer === null) {
        $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
            ->get(Serialize\SerializerInterface::class);
    }
    return $this->serializer;
}

The preference for the SerializerInterface was added since 2.2.x version of the Magento, and declared in the app/etc/di.xml:

<preference for="Magento\Framework\Serialize\SerializerInterface" type="Magento\Framework\Serialize\Serializer\Json" />

So I think your cache is old or the preference for the SerializerInterface is not working. Try to debug this issue by calling the Magento\Framework\Serialize\SerializerInterface (using dependency injection) somwhere in code, and check which class returned by di:

public function __construct(\Magento\Framework\Serialize\SerializerInterface $serializer) 
{ 
    echo get_class($serializer);
}

If it returns not an instance of the Magento\Framework\Serialize\Serializer\Json class returned - try to search this overwritten preference in the project and remove it.

If you are working on the remote server - firstly check the app/etc/di.xml file on the server directly.

Another way you can temporarly modify the core Json serializer and check which error returned:

Open the magento/framework/Serialize/Serializer/Json.php and change this method from:

/**
 * {@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;
}

to:

/**
 * {@inheritDoc}
 * @since 100.2.0
 */
public function serialize($data)
{
    $result = json_encode($data);
    if (false === $result) {
        switch (json_last_error()) {
            case JSON_ERROR_NONE:
                $error = ' - No errors';
                break;
            case JSON_ERROR_DEPTH:
                $error = ' - Maximum stack depth exceeded';
                break;
            case JSON_ERROR_STATE_MISMATCH:
                $error = ' - Underflow or the modes mismatch';
                break;
            case JSON_ERROR_CTRL_CHAR:
                $error = ' - Unexpected control character found';
                break;
            case JSON_ERROR_SYNTAX:
                $error = ' - Syntax error, malformed JSON';
                break;
            case JSON_ERROR_UTF8:
                $error = ' - Malformed UTF-8 characters, possibly incorrectly encoded';
                break;
            default:
                $error = ' - Unknown error';
                break;
        }
        throw new \InvalidArgumentException('Unable to serialize value. Error: ' . $error);
    }

    return $result;
}

Then you can see after the exception message a json error. May be your data is broken. keep in mind that all the old data should be unserialized and serialized using json in the setup upgrade scripts during magento update.

PS: do not forget to revert back the core files after debugging complete! The better way is use xDebug for that purpose.

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.