1. Go to this page and download the library: Download andreamk/jsonserialize 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/ */
andreamk / jsonserialize example snippets
\Amk\JsonSerialize\Autoloader::register();
use Amk\JsonSerialize\JsonSerialize;
$json = JsonSerialize::serialize($value);
$value = JsonSerialize::unserialize($json);
public static JsonSerialize::serialize(mixed $value, int $flags = 0, int $depth = 512): string|false
public static JsonSerialize::unserialize(string $json, int $depth = 512, int $flags = 0): mixed
public __sleep(): array
public __wakeup(): void
public __serialize(): array
public __unserialize(array $data): void
class MyClass extends \Amk\JsonSerialize\AbstractJsonSerializable {
}
$obj = new MyClass();
$json = json_encode($obj);
use Amk\JsonSerialize\JsonSerialize;
$json = JsonSerialize::serialize(
$value,
JSON_PRETTY_PRINT | JsonSerialize::JSON_SKIP_CLASS_NAME
);
public static serializeToData(
mixed $value,
$flags = 0
): mixed
[
'' => type, // itendifies the root element
'prop' => type, // identifies the level 1 property ($val->prop or $val['prop'])
'v1/v2/v3' => type, // identifies the level 3 property ($val->v1->v2->v3 or $val['v1']['v2']['v3'])
'v1/*' => type, // identifies all the properties that are children of v1,
'v1/*/v3' => type, // identifies all v3 properties of the children of v1
]
$map = new JsonUnserializeMap(
[
'' => 'cl:' . MyClass::class, // root val is an istance of MyClass
'items/*' => '?cl:' . MyItems::class, // all element of items are istances of MyItems or null
'items/*/parent' => 'rf:', // all prop parent of all items ar a reference to root value
'obj' => '?object' // obj can be null or an object
]
);
namespace Test;
class MyClass {
public $publicProp = 'public';
protected $protectedProp = 'protected';
private $privateProp = 'private';
private $arrayProp = [1,2,3];
}
$object = new MyClass();
$json = JsonSerialize::serialize($object, JSON_PRETTY_PRINT);
class MyClass {
public $propA = 'init A';
public $propC = 'init C';
}
$obj = JsonSerialize::unserialize($json);
var_dump($obj);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.