2017 © Pedro Peláez
 

library symfony-serializer-arrayobject-normalizer

ArrayObject Normalizer for Symfony/Serializer component. This Normalizer works with ArrayObject objects and its subclasses.

image

bartoszbartniczak/symfony-serializer-arrayobject-normalizer

ArrayObject Normalizer for Symfony/Serializer component. This Normalizer works with ArrayObject objects and its subclasses.

  • Sunday, February 5, 2017
  • by BartoszBartniczak
  • Repository
  • 1 Watchers
  • 0 Stars
  • 52 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

BartoszBartniczak\Symfony-Serializer-ArrayObject-Noramlizer Build Status Coverage Status

ArrayObject Normalizer for Symfony/Serializer component. This Normalizer works with ArrayObject objects and its subclasses.

Configuration

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use BartoszBartniczak\SymfonySerializer\Normalizer\ArrayObjectNormalizer;


$normalizers = [
    new ArrayObjectNormalizer(), //add ArrayObjectNoralizer to the normalizers array
    new ObjectNormalizer(),
    ];

$encoders = [
    new JsonEncoder()
];

$serializer = new Serializer($normalizers, $encoders);

Examples

Simple ArrayObject (De-)Serialization

$json = $serializer->serialize(new \ArrayObject(['a' => 1, 'c' => 3, 'e' => 5, 'g' => 7]), 'json');

In the $json variable you should contains now this JSON document:, (*1)

{
  "a": 1,
  "c": 3,
  "e": 5,
  "g": 7
}

Now you can deserialize this JSON object back to \ArrayObject:, (*2)

$serializer->deserialize($json, \ArrayObject::class, 'json');

Array Of Objects (De-)Serialization

If the \ArrayObject contains objects of some class, you need to define the type for deserialization., (*3)

$arrayOfObjects = new \ArrayObject([
  'einstein' => new Person('Albert Einstein'),
  'tesla' => new Person('Nikola Tesla')
]);

$json = $serializer->serialize($arrayOfObjects, 'json');

// deserialization
$deserializedObject = $serializer->deserialize($json, \ArrayObject::class.'<Person>', 'json');

Subclasses (extending the \ArrayObject class)

This Normalizer supports inheritance of objects. You can extend the \ArrayObject (e.g. for adding some methods) and this Normalizer still will be able to (de-)serialize objects., (*4)

<?php

class PersonArray extends \ArrayObject{

}

$arrayOfObjects = new PersonArray([
  'einstein' => new Person('Albert Einstein'),
  'tesla' => new Person('Nikola Tesla')
]);

$json = $serializer->serialize($arrayOfObjects, 'json');

// deserialization
$deserializedObject = $serializer->deserialize($json, PersonArray::class.'<Person>', 'json');

For other examples, you should check out the integration tests., (*5)

Tests

Unit tests

To run unit test execute the command:, (*6)

php vendor/phpunit/phpunit/phpunit --configuration tests/unit-tests/configuration.xml

Integration tests

To run integration tests execute the command:, (*7)

php vendor/phpunit/phpunit/phpunit --configuration tests/integration-tests/configuration.xml

The Versions

05/02 2017

dev-master

9999999-dev

ArrayObject Normalizer for Symfony/Serializer component. This Normalizer works with ArrayObject objects and its subclasses.

  Sources   Download

Apache 2.0

The Requires

 

The Development Requires

by Bartosz Bartniczak