Skip to content

Serializing

BowlOfSoup edited this page Sep 16, 2020 · 3 revisions

Serializing will transform an object, class or array of data into another format, for example: JSON or XML. You can serialize an associative array with plain data, an array of class, or a single class.

If you want to normalize an object/class it will normalize those object/classes first, the "Normalize" annotations are used. See Normalizing for details on that.

Encoding

For serialization two encodings are supported: JSON and XML.

Use statement and alias

On top of the object you want to serialize:

use BowlOfSoup\NormalizerBundle\Annotation as Bos;

Calling the serializer

The serializer can be injected, but also auto-wired.

<argument type="service" id="bos.serializer" />

Calling the serializer with a group is optional, but certainly recommended.

$result = $this->serializer->serialize($someEntity, EncoderFactory::TYPE_JSON, some-optional-group');

The result will be a string of data.

Annotation options

See Serialize annotations for custom options.