JMS Converter
PHP Converters Serializers Deserializers
Installation
composer require ecotone/jms-converter
Ecotone comes with integration with JMS Serializer and extending it with extra features.
Module Powered By
Great library, which allow for advanced conversion between types JMS/Serializer.
Native Conversion
Ecotone with JMS will do it's best to deserialize your classes without any additional configuration needed. Suppose we have JSON like below:
Then, suppose we have endpoint
with following Command:
No need for any configuration, deserialization and serialization will be handled for you.
Array Deserialization
In order to deserialize array you must provide type hint.
This will let JMS know, how to deserialize given array. In order to deserialize array of scalar types:
If your array is hash map however:
If you've mixed array containing scalars, then you may use ArrayObject to deserialize and serialize it preserving keys and types.
Custom Conversions To Classes
The difference between Native Conversion is that you take control of deserialization mechanism for specific class. You may call factory method, which will validate correctness of the data or you may provide some default based on your business logic.
Besides you may find it useful when there is a need to make conversion from class
to simple type like string
or int
.
JMS Converter
make use of Converters registered as Converters in order to provide all the conversion types described in Conversion Table. You can read how to register newConverter
in Conversion section.
Example usage
If we want to call bus with given JSON
and deserialize productIds
to UUID
:
Then, suppose we have endpoint with following Command:
We do not need to add any metadata describing how to convert JSON to PlaceOrder PHP class.
We already have it using type hints.
The only thing, that we need is to add how to convert string to UUID. We do it using Converter:
And that's enough. Whenever we will use string to UUID
conversion or string[] to UUID[]
. This converter will be automatically used.
Custom Conversions from Classes
Above example was for deserialization, however if you want to make use of serialization, then Converter from UUID
to string
is needed.
Serialization Customization
If you want to customize serialization or deserialization process, you may use of annotations on properties, just like it is describes in Annotation section in JMS Serializer.
Configuration
Register Module Conversion
withDefaultNullSerialization
Should nulls be serialized (bool, default: false
)
withNamingStrategy
Serialization naming strategy (string "identicalPropertyNamingStrategy" || "camelCasePropertyNamingStrategy",
default: "identicalPropertyNamingStrategy"
)
Serialize Nulls for specific conversion
If you want to make convert nulls for given conversion, then you can provide Media Type parameters
Conversion Table
JMS Converter
can handle conversions:
Last updated