A
A
Alexey Skobkin2016-03-22 00:36:19
symfony
Alexey Skobkin, 2016-03-22 00:36:19

How to implement nested object hierarchy deserialization with Symfony Serializer?

Here is a simplified code example:

class PostsPage
{
    /**
     * @var MetaPost[]
     * @Serializer\Groups({"import_post_page"})
     */
    private $posts;
// ...
}

class MetaPost
{
    /**
     * @var Recommendation
     * @Serializer\Groups({"import_post_page"})
     */
    private $rec;

    /**
     * @var Post
     * @Serializer\Groups({"import_post_page"})
     */
    private $post;
// ...
}

// ...
// Для сокращения количества кода сеттеры и геттеры как и прочие свойства были убраны

Naturally, there is JSON, in which everything corresponds to this structure. Question: is it possible to expand this JSON into a full-fledged data structure given the classes? More precisely, if possible, how specifically to indicate to the serializer from where to which data type it should be expanded. If you just follow the example from the documentation like this:
$data = $serializer->deserialize($json, 'SomeNamespace\PostsPage', 'json');

That $datawill be an instance of the class PostsPage. But in $postsit it will just have a multidimensional array, and not an array of type objects MetaPost(actually, I did not expect a miracle with reading @varfrom annotations).
In the official documentation, I didn’t see much instructions on how to deserialize the object hierarchy, and JMS Serializer pops up on search queries, which I already know how to set up, but I’m wondering how to get rid of the extra dependency of not the first freshness, given that Symfony has there is a component aimed at these tasks.
UPD: I had a suspicion that it is possible to use callbacks for properties, but it is not clear how to completely describe all nested objects with callbacks, and secondly, it is somehow ugly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2016-03-22
@skobkin

I think the following couple of links will lead to the right searches
https://github.com/doctrine/DoctrineModule/blob/ma...
https://packagist.org/search/?q=serializer
Symfony's serializer by itself is essentially nothing can. (I mean out of the box performance, like JMS)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question