2017 © Pedro Peláez
 

library pj-freeze

PHP to JSON Serializer/Deserializer

image

maciej-sz/pj-freeze

PHP to JSON Serializer/Deserializer

  • Thursday, August 25, 2016
  • by maciej-sz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,222 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

pj-freeze

PHP to JSON Serializer/Deserializer, (*1)

Latest Version on Packagist ![Build Status][ico-travis] No dependencies ![MIT License][ico-license], (*2)

Features

  • Serialization to JSON
  • Unserialization from JSON
  • Circular-reference handling
  • Corresponding JavaScript library is available to read serialized data on the browser side

Usage

This package can be used to serialize and unserialize data objects that contain circular references., (*3)

Example: basic round-trip

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

Example: persisting round-trip

An additional step is required for the serialization result to be persisted: PHP's standard json_encode() function has to be called. The json_encode() won't produce "Recursion detected" error this time., (*4)

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

Example: circular reference

Using only json_encode():, (*5)

// 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"

Using PjFreeze:, (*6)

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

Installation

Via composer:, (*7)

composer require maciej-sz/pj-freeze

The Versions

25/08 2016

dev-master

9999999-dev

PHP to JSON Serializer/Deserializer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

json serialize deserialize

25/08 2016

0.2.1

0.2.1.0

PHP to JSON Serializer/Deserializer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

json serialize deserialize

23/08 2016

dev-revert_greedy

dev-revert_greedy

PHP to JSON Serializer/Deserializer

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

json serialize deserialize

23/08 2016

0.2.0

0.2.0.0

PHP to JSON Serializer/Deserializer

  Sources   Download

The Requires

  • php >=5.6

 

The Development Requires

json serialize deserialize