adagio/serializer

Simple zero-config (de)serialization API

0.1.0 2017-02-27 16:02 UTC

This package is auto-updated.

Last update: 2024-04-17 10:07:46 UTC


README

Latest Stable Version Build Status License Total Downloads

A PHP serializer that just works.

Installation

Install Composer and run the following command to get the latest version:

composer require adagio/serializer

Usage

<?php

use Adagio\Serializer\DumbSerializer;

class Foo
{
    private $myProperty = 123;

    /**
     * @var Bar
     */
    public $bar;
}

class Bar
{
    private $word1 = 'Hello';
    protected $word2 = ['world!'];
}

$foo = new Foo;
$foo->bar = new Bar;

$serializer = new DumbSerializer;

echo $json = $serializer->serialize($foo));

// Outputs:
// {
//     "myProperty": 123,
//     "bar": {
//         "word1": "Hello",
//         "word2": [
//             "world!"
//         ]
//     }
// }

print_r($serializer->deserialize($json));

// Outputs:
// stdClass Object
// (
//     [myProperty] => 123
//     [bar] => Array
//         (
//             [word1] => Hello
//             [word2] => Array
//                 (
//                     [0] => world!
//                 )
//
//         )
//
// )

print_r($serializer->deserialize($json, Foo::class));

// Outputs:
// Foo Object
// (
//     [myProperty] => 123
//     [bar] => Array
//         (
//             [word1] => Hello
//             [word2] => Array
//                 (
//                     [0] => world!
//                 )
//
//         )
//
// )