Download the PHP package anzusystems/serializer-bundle without Composer
On this page you can find all versions of the php package anzusystems/serializer-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anzusystems/serializer-bundle
More information about anzusystems/serializer-bundle
Files in anzusystems/serializer-bundle
Package serializer-bundle
Short Description Serializer bundle.
License Apache-2.0
Informations about the package serializer-bundle
AnzuSerializer
A fast & light serializer bundle for symfony.
Install
Usage
Simply inject AnzuSystems\SerializerBundle\Serializer
via constructor, and then:
Default format for DateTimeInterface
objects (de)serialization can be changed:
Attributes
To be able to (de)serialize objects, the property (or method) of that object must have AnzuSystems\SerializerBundle\Attributes\Serialize
attribute.
Built-in handlers
- Auto-resolved handlers based on type:
BasicHandler
(scalar values and null)DateTimeHandler
(date format configurable via settings)EnumHandler
(conversion between string andEnumInterface
)ObjectHandler
(conversion of whole objects, i.e. embeds)UuidHandler
(conversion of Symfony Uuids)
- Custom handlers:
EntityIdHandler
(conversion of IDs into entities and back)ArrayStringHandler
(CSV into array:'1,2,3'
or'a, b,c'
to[1, 2, 3]
or['a', 'b', 'c']
)
To force a specific handler (override the auto-resolved handler), just specify the handler in the AnzuSerialize
attribute.
Custom handler.
To create a custom handler, simply extend the AnzuSystems\SerializerBundle\Handler\Handlers\AbstractHandler
.
For instance in the following example a Geolocation class is converted to/from array:
Then just force the handler to be used for the property via attribute:
In case you want always automatically all properties of the before-mentioned type Geolocation
to be handled by the GeolocationHandler
without forcing it via attribute, add following methods to the handler:
In case you want multiple automatic handlers that can both support the same thing, you can set priority with which the handler will be chosen. In that case, add the following method (higher priority will be chosen first):
By default, all handlers have priority 0. Except:
BasicHandler
has highest priority (10) - this handles simple scalar values, so generally you want it to be first.
ObjectHandler
has lowest priority (-1) - this handles nested iterables/objects that no other handler supports.
Automatically generated API documentation via NelmioApiDocBundle
Model describer will be automatically registered if NelmioApiDocBundle is present. Symfony annotations are also supported/reflected in documentation. DocBlock titles are also added automatically as description for properties and methods.
In case you create a custom handler, you can override the generated description by adding the following method to the handler:
Check out Property attribute for a list of supported description configuration options.
On top of that, you may want to add the NESTED_CLASS
key to replace the description with a whole another classes' description:
In case you want to define an array of particular objects, then:
It's best to have a look at the AnzuSystems\SerializerBundle\Handler\Handlers
namespace for inspiration on how other handlers work.
Caveats/requirements/features
- Iterables with keys will be automatically (de)serialized into an associative array or indexed collection.
- Currently, only json format is supported.
- Every property that you want to (de)serialize, must have a public getter and setter.
- Setter name example for property $email:
setEmail
- Getter name example for property $email:
getEmail
- Getter name example for boolean properties:
isEnabled
- Setter name example for property $email:
-
Constructor of an object that you want to (de)serialize cannot have required parameters.
- You can also use public static functions to instantiate an object if you want required parameters. For instance:
- Use
SerializeParam
to convert request body into desired object. Example:
All versions of serializer-bundle with dependencies
ext-json Version *
doctrine/common Version ^3.3
symfony/property-info Version ^6.3|^7.0