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:
1
{
2
"personName": "Johny",
3
"address": ["street": "A Good One", "houseNumber": 123
4
}
Copied!
1
$this->commandBus->sendWithRouting(
2
"settings.change",
3
'{"personName": "Johny","address":["street":"A Good One","houseNumber":123}',
4
"application/json"
5
)
6
Copied!
Then, suppose we have endpoint with following Command:
1
#[CommandHandler]
2
public function changeSettings(ChangeSettings $command)
3
{
4
// do something
5
}
Copied!
1
class ChangeSettings
2
{
3
private string $personName;
4
private Address $address;
5
}
6
7
class Address
8
{
9
private string $street;
10
private string $houseNumber;
11
}
Copied!
No need for any configuration, deserialization and serialization will be handled for you.
For deserialization of array without any extra configuration, simply describe it using DocBlock
1
/**
2
* @var Product[]
3
*/
4
private array $products;
Copied!

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 Convertermake 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:
1
{
2
"productIds": ["104c69ac-af3d-44d1-b2fa-3ecf6b7a3558"],
3
"promotionCode": "33dab",
4
"quickDelivery": false
5
}
Copied!
1
$this->commandBus->sendWithRouting(
2
"order.place",
3
'{"productIds": ["104c69ac-af3d-44d1-b2fa-3ecf6b7a3558"], "promotionCode": "33dab", "quickDelivery": false}',
4
"application/json"
5
)
Copied!
Then, suppose we have endpoint with following Command:
1
#[CommandHandler]
2
public function placeOrder(PlaceOrder $command)
3
{
4
// do something
5
}
Copied!
1
class PlaceOrder
2
{
3
/**
4
* @var Uuid[]
5
*/
6
private array $productIds;
7
8
private ?string $promotionCode;
9
10
private bool $quickDelivery;
11
}
Copied!
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:
1
class ExampleConverterService
2
{
3
#[Converter]
4
public function convert(string $data) : Uuid
5
{
6
return Uuid::fromString($data);
7
}
8
}
Copied!
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.
1
class ExampleConverterService
2
{
3
#[Converter]
4
public function convert(Uuid $data) : string
5
{
6
return $data->toString();
7
}
8
}
Copied!
1
class PlaceOrder
2
{
3
/**
4
* @var Uuid[]
5
*/
6
private array $productIds;
7
8
private ?string $promotionCode;
9
10
private bool $quickDelivery;
11
}
12
13
$this->serializer->convertFromPHP(
14
new PlaceOrder(//construct),
15
"application/json"
16
)
17
18
=>
19
20
{"productIds": ["104c69ac-af3d-44d1-b2fa-3ecf6b7a3558"], "promotionCode": "33dab", "quickDelivery": false}
Copied!

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.
1
class GetOrder
2
{
3
/**
4
* @SerializedName("order_id")
5
*/
6
private string $orderId;
7
}
Copied!

Configuration

Register Module Conversion
1
class Configuration
2
{
3
#[ServiceContext]
4
public function getJmsConfiguration()
5
{
6
return JMSConverterConfiguration::createWithDefaults()
7
->withDefaultNullSerialization(false) // 1
8
->withNamingStrategy("identicalPropertyNamingStrategy"); // 2
9
}
10
}
Copied!

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
1
$this->serializer->convertFromPHP(
2
["id" => 1,"name" => null],
3
"application/json;serializeNull=true"
4
)
5
6
=>
7
8
{"id":1,"name":null}
Copied!

Conversion Table

JMS Converter can handle conversions:
1
// conversion from JSON to PHP
2
application/json => application/x-php | {"productId": 1} => new OrderProduct(1)
3
// conversion from PHP to JSON
4
application-x-php => application/json | new OrderProduct(1) => {"productId": 1}
5
6
// conversion from XML to PHP
7
application/xml => application/x-php | <productId>1</productId> => new OrderProduct(1)
8
// conversion from PHP to XML
9
application-x-php => application/xml | new OrderProduct(1) => <productId>1</productId>
10
11
// conversion from JSON to PHP Array
12
application/json => application/x-php;type=array | {"productId": 1} => ["productId": 1]
13
// conversion from PHP Array to JSON
14
application/x-php;type=array => application/json | {"productId": 1} => ["productId": 1]
15
16
// conversion from XML to PHP Array
17
application/xml => application/x-php;type=array | <productId>1</productId> => ["productId": 1]
18
// conversion from PHP Array to XML
19
application/x-php;type=array => application/xml | ["productId": 1] => <productId>1</productId>
Copied!
Last modified 8mo ago