zumba/json-serializer

Serialize PHP variables, including objects, in JSON format. Support to unserialize it too.

Installs: 35 547

Dependents: 5

Suggesters: 0

Stars: 57

Watchers: 10

Forks: 11

Open Issues: 2

2.1.0 2016-04-28 17:59 UTC

README

Software License Build Status Code Coverage Scrutinizer

This is a library to serialize PHP variables in JSON format. It is similar of the serialize() function in PHP, but the output is a string JSON encoded. You can also unserialize the JSON generated by this tool and have you PHP content back.

Supported features:

  • Encode/Decode of scalar, null, array
  • Encode/Decode of objects
  • Support nested serialization
  • Support not declared properties on the original class definition (ie, properties in stdClass)
  • Support object recursion
  • Closures (via 3rd party library. See details below)

Unsupported serialization content:

  • Resource (ie, fopen() response)

Limitations:

  • Binary String or malformed UTF8 strings (ie, resulsts from SELECT AES_ENCRYPT(:content, :key) as encrypted)
    • These strings will need to be properly handled by converting to hex using bin2hex or utf8_encode in the __sleep() method

This project should not be confused with JsonSerializable interface added on PHP 5.4. This interface is used on json_encode to encode the objects. There is no unserialization with this interface, differently from this project.

Json Serializer requires PHP >= 5.4

Example

class MyCustomClass {
    public $isItAwesome = true;
    protected $nice = 'very!';
}

$instance = new MyCustomClass();

$serializer = new Zumba\JsonSerializer\JsonSerializer();
$json = $serializer->serialize($instance);
// $json will contain the content {"@type":"MyCustomClass","isItAwesome":true,"nice":"very!"}

$restoredInstance = $serializer->unserialize($json);
// $restoredInstance will be an instance of MyCustomClass

How to Install

If you are using composer, install the package zumba/json-serializer.

$ composer require zumba/json-serializer

Or add the zumba/json-serializer directly in your composer.json file.

If you are not using composer, you can just copy the files from src folder in your project.

Serializing Closures

For serializing PHP closures you have to pass an object implementing SuperClosure\SerializerInterface. This interface is provided by the project SuperClosure. This project also provides a closure serializer that implements this interface.

Closure serialization has some limitations. Please check the SuperClosure project to check if it fits your needs.

To use the SuperClosure with JsonSerializer, just pass the SuperClosure object as the first parameter on JsonSerializer constructor. Example:

$toBeSerialized = array(
    'data' => array(1, 2, 3),
    'worker' => function ($data) {
        $double = array();
        foreach ($data as $i => $number) {
            $double[$i] = $number * 2;
        }
        return $double;
    }
);

$superClosure = new SuperClosure\Serializer();
$jsonSerializer = new Zumba\JsonSerializer\JsonSerializer($superClosure);
$serialized = $jsonSerializer->serialize($toBeSerialized);

PS: JsonSerializer does not have a hard dependency of SuperClosure. If you want to use both projects make sure you add both on your composer requirements.