This code not working in Magento 2.2, But it's working in Magento 2.1. I am facing this error

Notice: unserialize(); Error at offset 0 of 168 bytes in /var/www/html/M2/app/code/Vendor/Module/Block/MyBlock.php

$infoRequest = $item->getOptionByCode('info_all')->getValue();
$addtoCartreqest = unserialize($infoRequest);

1 Answer 1


Read More: blog.mageprince.com

Inject \Magento\Framework\Serialize\Serializer\Json class for serialize and unserialize values.

protected $serialize;

public function __construct(
    \Magento\Framework\Serialize\Serializer\Json $serialize,
) {
    $this->serialize = $serialize;

Now use $this->serialize

 $value = $this->serialize->unserialize($serializeData);

As per Magento 2.2 Release Note

Security enhancements

In general, we’ve removed serialize/unserialize from most the code to improve protection against remote code execution attacks. We’ve enhanced protection of code where use of object serialization or unserialization was unavoidable. Additionally, we’ve increased our use of output escaping to protect against cross-site scripting (XSS) attacks.

  • 1
    Good catch , +1 from me @Prince :)
    – Keyur Shah
    Sep 29, 2017 at 12:50
  • True.. Jignesh is also my guru @Prince , I was lucky to work with Jignesh and now you are lucky :)
    – Keyur Shah
    Oct 11, 2017 at 19:06
  • Yes @KeyurShah you are right :) Oct 12, 2017 at 4:48

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.