2017 © Pedro Peláez
 

library jsonfreeze

JSON Serialization library

image

mindplay/jsonfreeze

JSON Serialization library

  • Monday, January 16, 2017
  • by mindplay.dk
  • Repository
  • 2 Watchers
  • 23 Stars
  • 8,058 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 10 Versions
  • 9 % Grown

The README.md

mindplay/jsonfreeze

Serialize and unserialize a PHP object-graph to/from a JSON string-representation., (*1)

Build Status, (*2)

Overview

This library can serialize and unserialize a complete PHP object-graph to/from a JSON string-representation., (*3)

This library differs in a number of ways from e.g. json_encode(), serialize(), var_export() and other existing serialization libraries, in a number of important ways., (*4)

Please see here for detailed technical background information., (*5)

The most important thing to understand, is that this library is designed to store self-contained object-graphs - it does not support shared or circular object-references. This is by design, and in-tune with good DDD design practices. An object-graph with shared or circular references cannot be stored directly as JSON, in a predictable format, primarily because the JSON data-format is a tree, not a graph., (*6)

Usage

Nothing to it., (*7)

use mindplay\jsonfreeze\JsonSerializer;

$serializer = new JsonSerializer();

// serialize to JSON:

$string = $serializer->serialize($my_object);

// rebuild your object from JSON:

$object = $serializer->unserialize($string);

Custom Serialization

You can define your own un/serialization functions for a specified class:, (*8)

$serializer = new JsonSerializer();

$serializer->defineSerialization(
    MyType::class,
    function (MyType $object) {
        return ["foo" => $object->foo, "bar" => $object->bar];
    },
    function (array $data) {
        return new MyType($data["foo"], $data["bar"]);
    }
);

Note that this works only for concrete classes, and not for abstract classes or interfaces - serialization functions apply to precisely one class, although you can of course register the same functions to multiple classes., (*9)

Date and Time Serialization

The DateTime and DateTimeImmutable classes have pre-registered un/serialization functions supporting a custom format, in which the date/time is stored in the common ISO-8601 date/time format in the UTC timezone, along with the timezone ID - for example:, (*10)

{
    "#type": "DateTime",
    "datetime": "1975-07-07T00:00:00Z",
    "timezone": "America\/New_York"
}

The Versions

16/01 2017

dev-master

9999999-dev

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

The Development Requires

16/01 2017

0.3.3

0.3.3.0

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

The Development Requires

13/09 2016

0.3.2

0.3.2.0

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

The Development Requires

20/07 2016

1.3.1

1.3.1.0

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

The Development Requires

07/06 2016

1.3.0

1.3.0.0

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

The Development Requires

16/10 2014

1.2.0

1.2.0.0

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

26/09 2014

1.1.0

1.1.0.0

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

26/09 2014

1.0.3

1.0.3.0

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

11/09 2014

1.0.2

1.0.2.0

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0

 

02/05 2013

1.0.1

1.0.1.0

JSON Serialization library

  Sources   Download

LGPL-3.0+

The Requires

  • php >=5.3.0