PHP code example of maciej-sz / pj-freeze

1. Go to this page and download the library: Download maciej-sz/pj-freeze 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/ */

    

maciej-sz / pj-freeze example snippets


use MaciejSz\PjFreeze\PjFreeze;

$Freeze = new PjFreeze();

$data = ["foo", "bar", "baz"];

$SerializationResult = $Freeze->serialize($data);
$serializedObj = $SerializationResult->jsonSerialize();

$unserialized = $Freeze->unserialize($serializedObj);
assert($data == $unserialized);

use MaciejSz\PjFreeze\PjFreeze;

$Freeze = new PjFreeze();

$data = ["foo", "bar", "baz"];
$serializedObj = $Freeze->serialize($data)->jsonSerialize();
$serialized_str = json_encode($serializedObj);

file_put_contents("/tmp/data.json", $serialized_str);
// ...
$contents_str = file_get_contents("/tmp/data.json");
$unserialized = $Freeze->unserializeJson($contents_str);
assert($data == $unserialized);

// WARNING: this is an example of how NOT to encode circular references
use MaciejSz\PjFreeze\PjFreeze;

$data = new \stdClass();
$data->recursion = $data; // circular reference

$raw_encoded = json_encode($data);
echo json_last_error_msg(); // "Recursion detected"

use MaciejSz\PjFreeze\PjFreeze;

$data = new \stdClass();
$data->recursion = $data; // circular reference

$Freeze = new PjFreeze();

$serializedObj = $Freeze->serialize($data)->jsonSerialize();
$jp_freeze_encoded = json_encode($serializedObj);
echo json_last_error_msg(); // "No error"

$unserializedObj = json_decode($jp_freeze_encoded);
$unserialized = $Freeze->unserialize($unserializedObj);
assert($unserialized->recursion === $unserialized);