PHP code example of pyther / json

1. Go to this page and download the library: Download pyther/json 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/ */

    

pyther / json example snippets


    // creates a new order class and populate its properties from a json string or array. 
    $order = Json::deserialize($json, Order::class);

    // creates a json string populate from orders properties.
    $json = Json::serialize($order);

$settings = new JsonSettings();

// set optional naming policy, default is none (json attrbiutes equals object attribute names).
// supported policies: CamelToPascalNamingPolicy, CamelToSnakeNamingPolicy, CamelToKebabNamingPolicy, PascalToCamelNamingPolicy
$settings->setNamingPolicy(new CamelToPascalNamingPolicy());

// Disable json indention (enabled by default).
$settings->setPrettyPrint(false);

// Enable or disable to serialize DateTime as string (enabled by default).
$settings->setDateTimeAsString(false);

// Set the default date time format (\DateTime::W3C by default).
// This can be overwriten per Property using "#[JsonDateTime(...)]"
$settings->setDateTimeFormat(\DateTime::W3C)

// Defines the default serialization format for enumerations (EnumFormat::Value by default). 
// This can be overridden using the "JsonEnum" meta tag.
// values are: EnumFormat::Value, EnumFormat::Name, EnumFormat::Full
$settings->setEnumFormat(EnumFormat::Value)

// Define to skip null values (false by default).
$settings->setSkipNull(true);

// Define to skip empty arrays (false by defaut).
$settings->setSkipEmptyArray(true);

// Set to true, to 

enum Status : int {
    case Inactive = 0;
    case Active = 1;
}


class EnumTest {
    #[JsonEnum(EnumFormat::Name)]
    public Status $status;
}

// or set global for all enums
$settings->setEnumFormat(EnumFormat::Name);

class EnumTest {
    #[JsonEnum(EnumFormat::Value)]
    public Status $status;
}

// or set global for all enums
$settings->setEnumFormat(EnumFormat::Value);

class EnumTest {
    #[JsonEnum(EnumFormat::Full)]
    public Status $status;
}

// or set global for all enums
$settings->setEnumFormat(EnumFormat::Full);

use Pyther\Json\Attributes\Json;

class MyClass
{
    // fill the "sku" from the json "id" property.    
    #[Json("id)]
    public string $sku;
}

use Pyther\Json\Attributes\JsonIgnore;

class MyClass
{
    // ignore on serialization and deserialization
    #[JsonIgnore]
    public string $ignoreMe;

    // ignore on serialization only
    #[JsonIgnore(true, false)]
    public string $ignoreMe;

    // ignore on deserialization only
    #[JsonIgnore(false, true)]
    public string $ignoreMe;
}

use Pyther\Json\Attributes\JsonType;

class MyClass
{
    // definfes the type of the array items.
    #[JsonType(OrderItem::class)]
    public array $typedArrayByMeta;

    // possible to replace the missing build in datatype.
    #[JsonType(\int::class)]
    public $intByMeta;
}

use Pyther\Json\Attributes\JsonDateTime;

class MyClass
{
    // parse this property by the given format.
    #[JsonDateTime("d/m/Y")]
    public string $dayOfBirth;
}

use Pyther\Json\Attributes\JsonEnum;

class MyClass
{
    // parse this property by the given format.
    #[JsonEnum(EnumFormat::Name)]
    public Status $status;
}

use Pyther\Json\Attributes\JsonComplete;

class MyClass
{
    // ...

    // Any parameterless function with any name you want. 
    #[JsonComplete]
    public function onComplete() {
        // $this is fully parsed and ready to use here
    }
}