Skip to content

Instantly share code, notes, and snippets.

@svolobuev
Created February 26, 2016 06:58
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svolobuev/ecd931f27982ef6f6ad8 to your computer and use it in GitHub Desktop.
Save svolobuev/ecd931f27982ef6f6ad8 to your computer and use it in GitHub Desktop.
JMS Serializer Subscriber example
services:
serializer.subscriber.news:
class: AppBundle\Serializer\Subscriber\NewsSubscriber
arguments: [@vich_uploader.storage, @liip_imagine.cache.manager, @request_stack]
tags:
- { name: jms_serializer.event_subscriber }
<?php
namespace AppBundle\Serializer\Subscriber;
use AppBundle\Entity\News;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\HttpFoundation\RequestStack;
use Vich\UploaderBundle\Storage\StorageInterface;
class NewsSubscriber implements EventSubscriberInterface
{
/**
* @var CacheManager
*/
private $imagineCacheManager;
/**
* @var StorageInterface
*/
private $storage;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @param StorageInterface $storage
* @param CacheManager $imagineCacheManager
*/
public function __construct(StorageInterface $storage, CacheManager $imagineCacheManager, RequestStack $requestStack)
{
$this->imagineCacheManager = $imagineCacheManager;
$this->storage = $storage;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
[
'event' => 'serializer.post_serialize',
'method' => 'onPostSerialize',
'class' => News::class,
],
];
}
public function onPostSerialize(ObjectEvent $event)
{
/** @var News $object */
$object = $event->getObject();
$uri = $this->storage->resolveUri($object, 'imageFile');
$request = $this->requestStack->getCurrentRequest();
$thumbUrl = null;
$url = null;
if ($uri) {
$thumbUrl = $this->imagineCacheManager->getBrowserPath($uri, 'news_image_api');
$url = $this->imagineCacheManager->getBrowserPath($uri, 'news_image');
}
/** @var \JMS\Serializer\JsonSerializationVisitor $visitor */
$visitor = $event->getVisitor();
$visitor->addData('image_thumb', $thumbUrl);
$visitor->addData('image', $url);
}
}
@NikitaKharkov
Copy link

NikitaKharkov commented Jan 11, 2018

Thank you very much! It's important because there are no examples in documentation!

@yfrommelt
Copy link

$visitor->addData('image_thumb', $thumbUrl);
is an old version of JMS, now use
$visitor->visitProperty(new StaticPropertyMetadata('', 'image_thumb', null), $thumbUrl);

@foulong
Copy link

foulong commented Jan 30, 2020

Thanks.
I'm agree with @NikitaKharkov => "no examples in documentation" -> no easy to use JMS Serializer !

To complete @yfrommelt, for JMS serializer 3.x (3.4 for me) :
Into \JMS\Serializer\JsonSerializationVisitor.php class (vendor\JMS\src folder), one method exists called "setData".
However, this method is deprecated, and with this comment :

@deprecated Use `::visitProperty(new StaticPropertyMetadata('', 'name', 'value'), 'value')` instead
     *
     * Allows you to replace existing data on the current object element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment