JMS Converter

PHP Converters Serializers Deserializers

Installation

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:

{
    "personName": "Johny",
    "address": ["street": "A Good One", "houseNumber": 123
}
$this->commandBus->sendWithRouting(
   "settings.change", 
   '{"personName": "Johny","address":["street":"A Good One","houseNumber":123}',
   "application/json"
)

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 for Classes

With Native Conversion, we take full control of how specific classes are serialized and deserialized. We can call factory methods that validate data correctness, apply business rules, or set intelligent defaults based on our domain logic. This is especially useful when converting between complex objects and simple types like strings or integers—for example, turning a Money value object into an integer for storage, or reconstructing it with proper validation when loading, ensuring given Object is always in valid state.

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 Handler:

and related Command:

Then as product Ids is array of Uuid, we can use customer Converter to define how to convert from string to Uuid, and vice versa (if 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 (default: false)

withNamingStrategy

Serialization naming strategy ("identicalPropertyNamingStrategy"/"camelCasePropertyNamingStrategy", default: "identicalPropertyNamingStrategy")

withDefaultEnumSupport

When enabled, default enum converter will be used, therefore Enums will serialize to simple types (default: false)

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

Was this helpful?