Skip to content

Latest commit

 

History

History
92 lines (71 loc) · 2.05 KB

LeveragingTheSerializer.md

File metadata and controls

92 lines (71 loc) · 2.05 KB

Speaker: Hugo Hamon -- @hhamon https://twitter.com/hhamon

Slides: https://speakerdeck.com/u/hhamon/p/leveraging-the-serializer-component

Leveraging the Serializer Component

How it works:

  • Start as an object
    • Pass through as an array
    • Encode to types:
      • XML
      • JSON
      • etc...
  • Deserialize, start as a type
    • Pass through as an array
    • Decode to an object

Normalizer interface

{images}

  • NormalizerInterface normalizes objects to arrays, scalars
  • Denormalizer does the reverse.

Encoder Interface

  • EncoderInterface encodes data into the given format
  • DecoderInterface decodes a string into PHP data

Including in symfony

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\GerSetMethodNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\SerializerEncoder\XmlEncoder;

// TODO: Incomplete code

Using

$serializer
	->serialize($object, 'xml');
<response>
	<id/>
	<customer/>
	<reference><...</reference>
	<arrival>...</arrival>
	<!-- ... -->
</response>

Ignoring attributes

$ignore = array('id', 'customer');
// TODO: Code incomplete

Unserialize a string

$xml = '<response>...</response>';
// Convert an XML string to an array
$data = $serializer->decode($xml, 'xml');

// Convert the array to an object
$class = 'ns'
// TODO: code incomplete

Adding support for YAML

{image} (not even trying. wait for slides.)

Too fast to type, take note:

At this point, the slides are too intensive to take notes, please wait for the slides to be published.

  • JMS Serializer Bundle
  • Twig Helpers
  • Customizing the XML root node
  • Serializing an object to a single XML node
  • Excluding or Exposing properties
  • Managing object properties, versioning
  • Offering several views of a serialized object
  • Forcing the serializer to call getters & setters
  • Mapping a property to another getter of setter method
  • Customizing the order in which properties are serialized
  • Typing properties for a complete unserialization