Answer the question
In order to leave comments, you need to log in
Why does JMS Serializer ignore entity-mapping files during manual serialization?
Good day,% habrauser%!
I use symphony for rest with the corresponding standard set of bundles. When simply returning an object as a result of the method execution, serialization proceeds correctly according to the mapping. But here it was necessary to serialize the entity by hand and faced with the fact that the mapping is completely ignored. And everything would be fine, it would be possible to set everything up with annotations, but my user class is inherited from Fosovsky, and you can’t add anything there. How to make mapping files work during manual serialization?
config:
jms_serializer:
metadata:
debug: %kernel.debug%
file_cache:
dir: "%kernel.cache_dir%/serializer"
auto_detection: true
directories:
FOSUserBundle:
namespace_prefix: FOS\UserBundle
path: "@ApiRestRestBundle/Resources/config/serializer/fosuser"
use JMS\Serializer\SerializerBuilder;
$serializer = SerializerBuilder::create()->build();
$ser_user = $serializer->serialize($user, 'json');
Answer the question
In order to leave comments, you need to log in
See, Symfony2 has such a thing as Dependency Injection . What's the point? The fact is that you configure all the services that you need in the config files, the config files are parsed by Symfony, and in the code you use cat services. Now let's look at your code:
jms_serializer:
....
use JMS\Serializer\SerializerBuilder;
$serializer = SerializerBuilder::create()->build();
$ser_user = $serializer->serialize($user, 'json');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question