gitnik / jms-typed-properties-driver

Allows you to use typed properties together with jms' serializer library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Warning

Do not use this in a production environment. While the base cases work, I am sure there are plenty of untested edge-cases

Usage

Deserialization:

class Make {
    private string $name;
}

class Car {
    private string $color;
    private Make $make;
}

$inputData = [
    'color' => 'red',
    'make' => [
        'name' => 'Tesla'
    ]
];

$serializer = SerializerBuilder::create()
    ->setMetadataDriverFactory(new TypedPropertiesDriverFactory())
    ->build();

$car = $serializer->deserialize(
   json_encode($inputData),
   Car::class,
   'json'

// interact with your Car object

);

Serialization:

class Make {
    public string $name;
}

class Car {
    public string $color;
    public Make $make;
}

$make = new Make();
$make->name = 'Tesla';

$car = new Car();
$car->make = $make;
$car->color = 'red';

$serializer = SerializerBuilder::create()
    ->setMetadataDriverFactory(new TypedPropertiesDriverFactory())
    ->build();

$json = $serializer->serialize(
   $car,
   'json'

/*
{
    "color": "red",
    "make": {
        "name": "Tesla"
    }
}
*/
);

How it works

JMS/Serializer has the concept of drivers, which are used to gather configuration information about an object it wants to (de)serialize. An example of this would be the Annotation driver, which reads configuration via annotations.

What this library does is, wrap whatever driver is currently being used and enhance the information acquired by the "default" driver with type information gathered via reflection.

This means you can gradually migrate towards type-hinted properties on a property-by-property basis and also mix different ways of configuration, like so:

class Car {
    /**
     * @SerializedName("colour") 
     */
    private string $color;
}

Limitations:

  1. While it can handle null values for (de)serialization, you are not allowed to omit properties during deserialization that have been typed-hinted, as those must always be initialized before they can be accessed
  2. There has been a long-standing issue with the parent library regarding coercing values instead of type-checking them (schmittjoh/serializer#561). This library has no influence on this behaviour

About

Allows you to use typed properties together with jms' serializer library


Languages

Language:PHP 97.5%Language:Dockerfile 1.6%Language:Makefile 0.9%