8

What is the most efficient way to convert my Symfony2 entity to an array ? Entity contains protected fields with setters/getters. Is it possible to do with JMSSerializer ?

5 Answers 5

31

Using this bundle is the most efficient way to convert Entities to serialized format. Moreover, it's recommended by Sensio Labs.

To serialize You need only to install, configure this bundle and then:

$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$serializer->serialize($object, 'json');

And deserialize:

$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$object = $serializer->deserialize($jsonData, 'MyNamespace\MyObject', 'json');

Nothing more.

You can also use it to convert an object to an array:

$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$array = $serializer->toArray($object);

Also, you can prevent infinite recursion using serialization groups:

$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$context = \JMS\Serializer\SerializationContext::create();
$context->setGroups($groups);
$serializer->serialize($object, 'json', $context);

Regards

Sign up to request clarification or add additional context in comments.

5 Comments

Sure, but you explained serialization to a json format. I would like to normalize it to an array.
I get: Cannot use object of type stdClass as array
@hsz $jmsSerializer = SerializerBuilder::create()->build(); $data = $jmsSerializer->toArray($entities);
Do not forget to add @Serializer\Expose annotation for properties.
It works fine with an ArrayCollection but when i try to use toArray() with a single entity I received this error: The input data of type "boolean" did not convert to an array, but got a result of type "boolean".. Do you know why?
13

If you have not installed Symfony Serializer Component.

install it composer require symfony/serializer

then just convert any entity to array as follows.

 $serializer = new Serializer(array(new ObjectNormalizer()));
 $data = $serializer->normalize($result, null, array('attributes' => 
   array('success','type','result','errorMessage')));

and the output will be,

$data = array:[ "success" => true "errorMessage" => null "result" => "1" "type" => "url" ]

Comments

10

Using JMSSerializer for such a simple task seems like an overkill to me. I would use Symfony Serializer Component. The demo page shows how to serialize an entity to JSON.

If you just want to put it to array, you don't need serialization at all, you could just instantiate GetSetMethodNormalizer and use it since component uses arrays as normalized format.

1 Comment

What about lazy-load many-to-many attributes? It's not working.
0

You can also just create a public routine in the entity itself which is similar to what serializer is doing.

Comments

0

You can use Serializer Component:

$person = new Person();
$person->setName('foo');
$person->setAge(99);

$serializer = new Symfony\Component\Serializer\Serializer([new ObjectNormalizer()]);
var_dump($serializer->normalize($person));

See the documentation: https://symfony.com/doc/current/components/serializer.html#serializing-an-object

Also, you can configure the DI globally

App.ObjectNormalizer:
    alias: Symfony\Component\Serializer\Normalizer\ObjectNormalizer

Symfony\Component\Serializer\Serializer:
    arguments:
        $normalizers:
            0: '@App.ObjectNormalizer'

and then simply call:

var_dump($serializer->normalize($person));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.