0

I'm trying to retrieve some data from a controller in a serialized format that I can unserialize later. For example, I have created an entity with the following data fields

MyEntity

  • entity_id
  • identifier
  • created_at
  • updated_at

In my controller's execution method I retrieve the list of results based on my criteria.

Now I would like to return a JSON response serialized object in this format

{result:[{'entity_id': 'id', 'identifier': 'identifier'},{...},{...}]}`

I'm trying to use the SerializerInterface to do this

serializer->serialize($item)

but serializing the entities gives empty values like this which is my first issue.

{"result":[{},{},{},{}]}

Later on, in another controller, I would have loved to do the following based on the results retrieved from the first controller which is my second issue.

unserialize($response->getBody()->getContents()->result)

And give me an array of MyEntity items

MyEntity[]

In Symfony, I can already do this using the serializer component which is very powerful and allows me to group attributes I want to expose and also ignore things I don't want to expose so I guess I'm looking for something similar. Is there a Magento 2 implementation for this?

https://symfony.com/doc/current/components/serializer.html

After further digging I noticed that the serializer for json executes a simple json_encode so there probably isn't any standard implementation for serializing/unserializing an object. The best I could do to serialize the Model was to implement in my model the \JsonSerializable interface like this

 public function jsonSerialize()
    {
        return [
          self::KEY_KEY => $this->getData(self::KEY_KEY),
          self::KEY_IDENTIFIER => $this->getData(self::KEY_IDENTIFIER),
          self::KEY_NAME => $this->getData(self::KEY_NAME),
          self::KEY_DESCRIPTION => $this->getData(self::KEY_DESCRIPTION),
        ];
    }

I would love to be able to utilize a framework method to do this though so I'm still looking for an answer to this as well as to how to decode the serialized string back to the Object's Class.

2

I highly suggest using \Magento\Framework\Serialize\Serializer\Json

5
  • Magento 2 has noted in the devdocs that this implementation should not be used directly any longer For security reasons, SerializerInterface implementations, such as the Json and Serialize classes, should not serialize and unserialize objects.
    – gabtzi
    Apr 20 '20 at 12:19
  • good point, I'd didn't know this Apr 20 '20 at 12:20
  • Yeah I know. They added it at some point along with this Magento discourages using the Serialize implementation directly because it can lead to security vulnerabilities. Always use the SerializerInterface for serializing and unserializing.
    – gabtzi
    Apr 20 '20 at 12:21
  • however "The main purpose of data serialization is to convert data into a string using serialize() to store in a database, a cache, or pass onto another layer in the application." is exactly what you want to do.... Apr 20 '20 at 12:22
  • Yes indeed, however it's still not able to handle Object serialization. If I pass the data as array it's gonna work using the interface or the implementation directly, but I would like to serialize the whole object in my case.
    – gabtzi
    Apr 20 '20 at 12:27
1
<?php

namespace Demo\Demo\Demo;

use Magento\Framework\Serialize\SerializerInterface;

class DemoClass
{
    /**
     * @var SerializerInterface
     */
    protected $serializer;

    public function __construct(SerializerInterface $serializer)
    {
        $this->serializer = $serializer;
    }

    public function arrayToJson($data)
    {
        return $this->serializer->serialize($data);
    }

    public function jsonToArray($data)
    {
        return $this->serializer->unserialize($data);
    }
}

I think you need something like above.

4
  • SerializerInterface is not always the jsonserializer Apr 20 '20 at 12:17
  • @PhilippSander Please explain more about it.
    – Msquare
    Apr 20 '20 at 12:18
  • So you're suggesting I create my own serializer / unserializer or add it to my models? I'm confused as this would basically serialize an array and not an object as I am trying to do.
    – gabtzi
    Apr 20 '20 at 12:22
  • use Magento\Catalog\Model\ResourceModel\Product\Collection; $this->productCollection->addFieldToFilter('status', '1')->toArray(); Something like above gives array to serialize then into JSON. Apr 20 '20 at 12:34
-1

Another option is writing something like this in a helper

public function decode($data)
{
    return \Zend_Json::decode($data);
}
2
  • This is a bad practice, you should use DI to import the class.
    – Nikola
    Aug 10 at 20:07
  • I disagree. For this safer and less likely to be affected by the framework changing the serialization method in future due to security concerns. Meanwhile magento is extremely unlikely to decoupled from zend Aug 10 at 21: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.