PHP code example of luverelle / pson

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);



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;
    }
}

$json = '{
    "name": "Jiang Xina",
    "email": "[email protected]",
    "rating": 999.99
}';
$user = PSON::fromJsonArrayAsClass(json_decode($json, true), User::class);
var_dump($user);

object(User)#2 (3) {
  ["name":"User":private]=>
  string(10) "Jiang Xina"
  ["email":"User":private]=>
  string(14) "[email protected]"
  ["socialRating":"User":private]=>
  float(999.99)
}



declare(strict_types=1);

use luverelle\pson\attributes\JsonProperty;
use luverelle\pson\PSON;

class Address{

    #[JsonProperty("post_code")]
    private int $postCode;

    #[JsonProperty("city")]
    private string $city;

    public function __construct(int $postCode, string $city){
        $this->postCode = $postCode;
        $this->city = $city;
    }
}

class Contact{

    #[JsonProperty("name")]
    private string $name;

    #[JsonProperty("address")]
    private Address $address;

    public function __construct(string $name, Address $address){
        $this->name = $name;
        $this->address = $address;
    }
}

$contact = new Contact("Elizabeth", new Address(350000, "Krasnodar"));
$jsonArray = PSON::toJsonArray($contact);
echo json_encode($jsonArray, JSON_PRETTY_PRINT) . PHP_EOL;
var_dump(PSON::fromJsonArrayAsClass($jsonArray, Contact::class));

{
    "name": "Elizabeth",
    "address": {
        "post_code": 350000,
        "city": "Krasnodar"
    }
}
object(Contact)#8 (2) {
  ["name":"Contact":private]=>
  string(9) "Elizabeth"
  ["address":"Contact":private]=>
  object(Address)#15 (2) {
    ["postCode":"Address":private]=>
    int(350000)
    ["city":"Address":private]=>
    string(9) "Krasnodar"
  }
}


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
}