1. Go to this page and download the library: Download luverelle/pson 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/ */
luverelle / pson example snippets
class User{
private int $id = 1; //won't be converted into JSON
#[JsonProperty("name")]
private string $name = "Daniel"; //will be converted into JSON like {"name": "Daniel"}
}
declare(strict_types=1);
use luverelle\pson\attributes\JsonProperty;
use luverelle\pson\PSON;
class User{
#[JsonProperty("name")]
private string $name;
#[JsonProperty("email")]
private string $email;
#[JsonProperty("rating")]
private float $socialRating;
public function __construct(string $name, string $email, float $socialRating){
$this->name = $name;
$this->email = $email;
$this->socialRating = $socialRating;
}
}
$user = new User("Jiang Xina", "[email protected]", 999.99);
$jsonArray = PSON::toJsonArray($user);
echo json_encode($jsonArray, JSON_PRETTY_PRINT);
class User{
//some stuff
}
class UserManager{
/**
* @var User[]
*/
#[JsonProperty("users", arrayValueClass: User::class)]
private array $cachedUsers; //note that phpdoc isn't necessary for json, it's for type hinting in IDE
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.