PHP code example of gitnik / jms-typed-properties-driver

1. Go to this page and download the library: Download gitnik/jms-typed-properties-driver library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gitnik / jms-typed-properties-driver example snippets


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

);

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"
    }
}
*/
);

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