description |
---|
PHP Converters Serializers Deserializers |
{% hint style="success" %}
composer require ecotone/jms-converter
{% endhint %}
Ecotone comes with integration with JMS Serializer and extending it with extra features.
Great library, which allow for advanced conversion between types JMS/Serializer.
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:
#[CommandHandler]
public function changeSettings(ChangeSettings $command)
{
// do something
}
class ChangeSettings
{
private string $personName;
private Address $address;
}
class Address
{
private string $street;
private string $houseNumber;
}
No need for any configuration, deserialization and serialization will be handled for you.
In order to deserialize array you must provide type hint.
/**
* @var Product[]
*/
private array $products;
This will let JMS know, how to deserialize given array.
In order to deserialize array of scalar types:
/** @var string[] */
private array $productNames;
=>
["milk","plate"]
If your array is hash map however:
/** @var array<string,string> */
private array $order;
=>
["productId":"fff123a","productName":"milk"]
If you've mixed array containing scalars, then you may use ArrayObject to deserialize and serialize it preserving keys and types.
private \ArrayObject $data;
=>
["name":"Johny","age":13,"passport":["id":123]]
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.
If we want to call bus with given JSON
and deserialize productIds
to UUID
:
{
"productIds": ["104c69ac-af3d-44d1-b2fa-3ecf6b7a3558"],
"promotionCode": "33dab",
"quickDelivery": false
}
$this->commandBus->sendWithRouting(
"order.place",
'{"productIds": ["104c69ac-af3d-44d1-b2fa-3ecf6b7a3558"], "promotionCode": "33dab", "quickDelivery": false}',
"application/json"
)
Then, suppose we have endpoint with following Command:
#[CommandHandler]
public function placeOrder(PlaceOrder $command)
{
// do something
}
class PlaceOrder
{
/**
* @var Uuid[]
*/
private array $productIds;
private ?string $promotionCode;
private bool $quickDelivery;
}
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:
class ExampleConverterService
{
#[Converter]
public function convert(string $data) : Uuid
{
return Uuid::fromString($data);
}
}
And that's enough. Whenever we will use string to UUID
conversion or string[] to UUID[]
. This converter will be automatically used.
Above example was for deserialization, however if you want to make use of serialization, then Converter from UUID
to string
is needed.
class ExampleConverterService
{
#[Converter]
public function convert(Uuid $data) : string
{
return $data->toString();
}
}
class PlaceOrder
{
/**
* @var Uuid[]
*/
private array $productIds;
private ?string $promotionCode;
private bool $quickDelivery;
}
$this->serializer->convertFromPHP(
new PlaceOrder(//construct),
"application/json"
)
=>
{"productIds": ["104c69ac-af3d-44d1-b2fa-3ecf6b7a3558"], "promotionCode": "33dab", "quickDelivery": false}
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.
class GetOrder
{
/**
* @SerializedName("order_id")
*/
private string $orderId;
}
Register Module Conversion
class Configuration
{
#[ServiceContext]
public function getJmsConfiguration()
{
return JMSConverterConfiguration::createWithDefaults()
->withDefaultNullSerialization(false) // 1
->withNamingStrategy("identicalPropertyNamingStrategy"); // 2
}
}
Should nulls be serialized (bool, default: false
)
Serialization naming strategy (string "identicalPropertyNamingStrategy" || "camelCasePropertyNamingStrategy",
default: "identicalPropertyNamingStrategy"
)
If you want to make convert nulls for given conversion, then you can provide Media Type parameters
$this->serializer->convertFromPHP(
["id" => 1,"name" => null],
"application/json;serializeNull=true"
)
=>
{"id":1,"name":null}
JMS Converter
can handle conversions:
// conversion from JSON to PHP
application/json => application/x-php | {"productId": 1} => new OrderProduct(1)
// conversion from PHP to JSON
application-x-php => application/json | new OrderProduct(1) => {"productId": 1}
// conversion from XML to PHP
application/xml => application/x-php | <productId>1</productId> => new OrderProduct(1)
// conversion from PHP to XML
application-x-php => application/xml | new OrderProduct(1) => <productId>1</productId>
// conversion from JSON to PHP Array
application/json => application/x-php;type=array | {"productId": 1} => ["productId": 1]
// conversion from PHP Array to JSON
application/x-php;type=array => application/json | {"productId": 1} => ["productId": 1]
// conversion from XML to PHP Array
application/xml => application/x-php;type=array | <productId>1</productId> => ["productId": 1]
// conversion from PHP Array to XML
application/x-php;type=array => application/xml | ["productId": 1] => <productId>1</productId>