Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

serialization - Add extra fields using JMS Serializer bundle

I've an entity I usually serialize using the JMS Serializer bundle. I have to add to the serialization some fields that doesn't reside in the entity itself but are gathered with some db queries.

My idea was to create a custom object, fill the fields with the entity fields and add the custom one. But this seems a bit tricky and expensive to do for every variation (I use lot of serialization groups) of the class.

Is there a better/standard way to do this? Using a factory? Pre/Post serialization events?

Maybe I can listen for the serialization and checking entity type and serialization groups add the custom fields? But instead of making a query for each entity it would be better to gather all the data of the related entities and then add it to them. Any help is appreciated

question from:https://stackoverflow.com/questions/15007281/add-extra-fields-using-jms-serializer-bundle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've found the solution by myself,

to add a custom field after the serialization has been done we've to create a listener class like this:

<?php

namespace AcmeDemoBundleListener;

use JMSDiExtraBundleAnnotationService;
use JMSDiExtraBundleAnnotationTag;
use JMSDiExtraBundleAnnotationInject;
use JMSDiExtraBundleAnnotationInjectParams;
use SymfonyComponentHttpKernelEventPostResponseEvent;
use AcmeDemoBundleEntityTeam;
use JMSSerializerHandlerSubscribingHandlerInterface;
use JMSSerializerEventDispatcherEventSubscriberInterface;
use JMSSerializerEventDispatcherPreSerializeEvent;
use JMSSerializerEventDispatcherObjectEvent;
use JMSSerializerGraphNavigator;
use JMSSerializerJsonSerializationVisitor;

/**
 * Add data after serialization
 *
 * @Service("acme.listener.serializationlistener")
 * @Tag("jms_serializer.event_subscriber")
 */
class SerializationListener implements EventSubscriberInterface
{

    /**
     * @inheritdoc
     */
    static public function getSubscribedEvents()
    {
        return array(
            array('event' => 'serializer.post_serialize', 'class' => 'AcmeDemoBundleEntityTeam', 'method' => 'onPostSerialize'),
        );
    }

    public function onPostSerialize(ObjectEvent $event)
    {
        $event->getVisitor()->addData('someKey','someValue');
    }
}

That way you can add data to the serialized element.

Instead, if you want to edit an object just before serialization use the pre_serialize event, be aware that you need to already have a variable (and the correct serialization groups) if you want to use pre_serialize for adding a value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...