GithubHelp home page GithubHelp logo

broadway-serialization's Introduction

Broadway Serialization helper library

By Matthias Noback

Event-sourcing framework Broadway requires a lot of your objects (like domain events and often view models as well) to be serializable because they have to be stored in plain text and later be reconstituted.

Making an object serializable requires you to write lots of pretty simple code. Even though that code is simple, you are likely to make mistakes in it. It's also a bit boring to write. To alleviate the pain, this library helps you to make objects serializable with in a few simple steps.

Installation

Just run

composer require matthiasnoback/broadway-serialization

Conventions

This library is very simple and it just works because of a few simple and yet assumptions:

  1. Serializable objects have at least one property. Properties can have any kind of scope.
  2. Serializable objects implements Broadway's Serializable interface.
  3. Properties contain scalar values, serializable objects, or arrays of serializable objects.

Example

use Broadway\Serializer\Serializable;
use BroadwaySerialization\Serialization\AutoSerializable;

class SerializableObject implements SerializableInterface
{
    use AutoSerializable;

    /**
     * @var string
     */
    private $foo;
    
    /**
     * @var SerializableObject
     */
    private $bar;
    
    /**
     * @var SerializableObject[]
     */
    private $bars = [];

    protected static function deserializationCallbacks()
    {
        return [
            'bar' => ['SerializableObject', 'deserialize'],
            'bars' => ['SerializableObject', 'deserialize']
        ];
    }
}

By implementing deserializationCallbacks() you can define callables that should be called in order to deserialize the provided data. The callable will be called once for single values or multiple times for arrays of values.

Setup

The Serializable trait depends on a little bit of setup. Before calling its deserialize() method, make sure you have properly set up a Reconstitute service, like this:

use BroadwaySerialization\Reconstitution\Reconstitution;
use BroadwaySerialization\Reconstitution\ReconstituteUsingInstantiatorAndHydrator;
use Doctrine\Instantiator\Instantiator;
use BroadwaySerialization\Hydration\HydrateUsingReflection;

Reconstitution::reconstituteUsing(
    new ReconstituteUsingInstantiatorAndHydrator(
        new Instantiator(),
        new HydrateUsingReflection()
    )
);

Note that this package ships with different implementations of the hydrator. Depending on your setup and preferences, you can also use the HydrateUsingClosure class. The former generally performs better than the HydrateUsingReflection.

Performance

When using this library, your personal performance will increase significantly. Of course, runtime performance will be worse (not noticeably though, unless you're actually deserializing millions of objects).

broadway-serialization's People

Contributors

bramdevries avatar haampie avatar jdrieghe avatar markredeman avatar matthiasnoback avatar mbadolato avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

broadway-serialization's Issues

Add deserialization for DateTime objects

DT objects will serialize to

[ "date": "2016-02-17 12:26:05.389122", "timezone_type": 1, "timezone": "+00:00" ]

and should deserialized automatically to a DT object again.

Symfony Integration

Is there any reason why symfony integration via bundle has been dropped?
It was trivial to set up Reconstitution::reconstituteUsing() this way.
Now i might have to write an extra EventListener/-Subscriber for this.

PHP 8 Compatibility

Since broadway/broadway 2.4 supports PHP 8, it might be useful to update this library as well.

I experimented a little bit already and it seems fairly straightforward except for 1 thing: the Symfony integration.

What strategy should we apply going forward?
My suggestion would be to remove the Symfony integration and rely on the framework independent configuration for this library moving forward. Additional libraries can easily be created to integrate with different frameworks, but as the configuration is just a couple of lines I'm not sure if it's worth it.

I would be happy to submit a PR that updates dev dependencies and adds PHP8 compatibility.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.