JMSSerializerBundle上的自定义处理程序将被忽略 [英] Custom Handler on JMSSerializerBundle is ignored

查看:56
本文介绍了JMSSerializerBundle上的自定义处理程序将被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自定义处理程序用于JMS序列化程序包

I am attempting to use a custom handler for JMS Serializer Bundle

class CustomHandler implements SubscribingHandlerInterface
{
    public static function getSubscribingMethods()
    {
        return array(
            array(
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
                'format' => 'json',
                'type' => 'integer',
                'method' => 'serializeIntToJson',
            ),
        );
    }

    public function serializeIntToJson(JsonSerializationVisitor $visitor, $int, array $type, Context $context)
    {
         die("GIVE ME SOMETHING");
    }
}

这什么也不做,也不会死.这就是我注册处理程序的方式

This does nothing, and does not die. This is how I am registering the handler

$serializer = SerializerBuilder::create()
    ->configureHandlers(function(HandlerRegistry $registry) {
        $registry->registerSubscribingHandler(new MyHandler());
    })
    ->addDefaultHandlers()
    ->build();

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

我的处理程序从不被调用,我无法在序列化时处理数据.

My handler is never called and I cannot manipulate the data on serialisation.

推荐答案

我有这个可行的方法

    $serializer = SerializerBuilder::create()
        ->configureListeners(function(EventDispatcher $dispatcher) {
            $dispatcher->addSubscriber(new ProjectSubscriber($this->container));
            $dispatcher->addSubscriber(new UserSubscriber($this->container));
        })
        ->addDefaultListeners()
        ->addMetadataDir(realpath($this->get('kernel')->getRootDir()."/../") . '/src/Jake/NameOfBundle/Resources/config/serializer')
        ->build();

    return $serializer->serialize($project, 'json');

$project是我的实体.

如果没有序列化程序配置,则可以省略此行

You can omit this line if you don't have serializer configs

->addMetadataDir(realpath($this->get('kernel')->getRootDir()."/../") . '/src/Jake/NameOfBundle/Resources/config/serializer')

我认为我的主要问题是这个->addDefaultListeners().

I think my main issue was this ->addDefaultListeners().

config.yml我有

jms_serializer:
    metadata:
        auto_detection: true
        directories:
            NameOfBundle:
                namespace_prefix: ""
                path: "@JakeNameOfBundle/Resources/config/serializer"

我没有设置任何东西来使JMS成为服务.

I don't have anthing set up to make JMS a service.

这篇关于JMSSerializerBundle上的自定义处理程序将被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆