1

I just complete my module n I was trying to test my module on different versions my module was working perfectly on 2.2.7 and 2.3.0 versions but when I tried on 2.1.16 then it gives me the error

Error filtering template: Class Magento\Framework\Serialize\Serializer\Json does not exist

How can I solve this error n what is the reason behind this error

0

The SerializerInterface interface and its implementations only exist since Magento version 2.2. Because of this, it is not possible to use these classes in code that has to be compatible with Magento 2.1 or 2.0. In code that is compatible with earlier versions of Magento 2, constructor dependency injection can not be used to get an instance of SerializerInterface. Instead, a runtime check if the SerializerInterface definition exists can made, and if it does, it can be instantiated by directly accessing the object manager using a static method. Alternatively a check against the Magento 2 version or the magento/framework composer package version would work, too. If the interface does not exist or an earlier version of Magento 2 is being executed, the appropriate native PHP serialization function has to be called, e.g. \serialize() or \json_encode(), depending on the usercase.

First, we need to check SerializerInterface class exist. If exist, use object manager to create an object and use it. If not exist, use native PHP function serialize() or json_encode(). Please refer the below code for your reference.

 private function serialize($data)
 {
    if (class_exists(\Magento\Framework\Serialize\SerializerInterface::class)) {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $serializer = $objectManager->create(\Magento\Framework\Serialize\SerializerInterface::class);
        return $serializer->serialize($data);
    }
    return \serialize($data);
}

Please refer this link for more details.

  • So Brother what is the solution did I need to use two different versions for 2.1.16 and another for 2.2.X or updated versions?? – Asad Khan Mar 12 at 14:48
  • check my updated answer. – Bilal Usean Mar 12 at 14:56

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.