Skip to content

Instantly share code, notes, and snippets.

@rparsi
Created July 9, 2018 19:06
Show Gist options
  • Select an option

  • Save rparsi/dc634b79c4921e5a59cf48cee6f1244d to your computer and use it in GitHub Desktop.

Select an option

Save rparsi/dc634b79c4921e5a59cf48cee6f1244d to your computer and use it in GitHub Desktop.
Handle differing serialized object/data formats between magento versions...because magento devs too stupid to do it correctly
<?php
namespace YourVendorName\FrameworkOverride\Framework\Serialize\Serializer;
use Magento\Framework\Serialize\Serializer\Json as Base;
class Json extends Base
{
public function unserialize($string) // override of base class method
{
$result = json_decode($string, true);
if (json_last_error() !== JSON_ERROR_NONE) {
// custom logic here: if format is not json, try something else
return $this->unserializeUsingPHP($string);
}
return $result;
}
// new method
public function unserializeUsingPHP($string)
{
if (!is_string($string)) {
throw new \InvalidArgumentException('Unable to unserialize value.');
}
try {
$result = \unserialize($string); // prior to json format, this is how magento (1 or 2) would deserialize
if ($result === FALSE) {
throw new \InvalidArgumentException('Unable to unserialize value.'); // following pattern in base class but you could use LocalizedException instead
}
return $result;
} catch (\Exception $exception) {
throw new \InvalidArgumentException('Unable to unserialize value.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment