Execute:
composer install
php index.php
Output:
object(MyList)#35 (0) {
["items"]=>
uninitialized(array)
}
object(MyList)#36 (0) {
["items"]=>
uninitialized(array)
}
{ | |
"name": "balthild/mwe", | |
"require": { | |
"crell/serde": "^1.1" | |
}, | |
"authors": [] | |
} |
<?php | |
use Crell\Serde\SerdeCommon; | |
require __DIR__ . '/vendor/autoload.php'; | |
readonly class MyList { | |
#[SequenceField(arrayType: MyItem::class)] | |
public array $items; | |
} | |
readonly class MyItem { | |
public int $id; | |
} | |
$serde = new SerdeCommon(); | |
// Trying JSON | |
$json = '[ | |
{ "id": 1 }, | |
{ "id": 2 } | |
]'; | |
$obj = $serde->deserialize($json, from: 'json', to: MyList::class); | |
var_dump($obj); | |
// Trying PHP array | |
$array = [ | |
[ "id" => 1 ], | |
[ "id" => 2 ] | |
]; | |
$obj2 = $serde->deserialize($array, from: 'array', to: MyList::class); | |
var_dump($obj2); |