magento / magento/community-features

allow serializer to serialize / deserialize objects

Open
#262 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Priority: P3
Dominant language
No language data
Stars
46
Forks
14
PR merge metrics
No merged PRs in 30d

Description

I've been digging into the code of Magento 2 in an attempt to serialize my Models for general use like
- for config data in javascript uiComponents
- for saving in the database
- for ajax requests

When it comes to JSON, currently the \Magento\Framework\Serialize\SerializerInterface is a very simple call to json_encode with an exception and nothing more.

I believe this is not enough for modern frameworks and it's very important to have the ability to serialize a Class Object and be able to deserialize a JSON string to a Class Object. This would effectively let us serialize Models, Collections, SearchResults, etc etc in a very simple way, convert them back and use all the available functions they have.

Symfony already does this since years ago with the serializer component https://symfony.com/doc/current/components/serializer.html and it's very practical.

It can
- handle private / public properties,
- you can annotate whatever you want to allow to be serialized
- you can group properties
- you can also handle circular references for example with linked entities. If we'd assume
`Product` and `Category`
You could serialize a product without going into the categories or even put a stop to it like this
Product -> Category[] -> Each Category -> Product[] -> Product -> and circular reference.

Now the underlying Magento structure is still using the `getData` and `setData` methods and not class properties thus making it a bit more difficult to implement so I don't think symfony's serializer would be easy to integrate as we'd need to rewrite various functions to make it work, however, would it be possible to extend the SerializerInterface to utilize the functions `toJson` and `convertToJson` from the `\Magento\Framework\DataObject`

Then we could serialize Models and Collections by invoking that underlying method in one command and deserialize back to it. Deserialization would need an additional Interface which would allow it to to deserialize the data into a specific Model or Collection.

I'd love to hear your thoughts about this.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing Magento\Framework\Serialize\SerializerInterface and the Magento\Framework\DataObject methods toJson and convertToJson, then compare the requested behavior with Symfony's Serializer component. The issue does not name implementation files or tests; done would require an agreed serialization and deserialization interface for Models and Collections, including the scope of circular references and data selection.

Written by the indexing model from the issue text.

Assessment

Tech stack
json, symfony
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.