将自定义属性添加到序列化对象 [英] Add custom property to serialized object

查看:254
本文介绍了将自定义属性添加到序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Web服务开发RESTful API.而且我需要公开一些不属于实体本身的属性.

I'm developing RESTful API for a web service. And I need to expose some properties that do not belong to an entity itself.

例如,我有一个Pizza实体对象,它具有自己的sizename属性.我正在使用 FOSRestBundle JMSSerializer 以JSON格式输出它.我已经为此实体设置了属性注释,以通过序列化组公开所需的属性,并且效果很好.

For example I have a Pizza entity object, it has it's own size and name properties. I'm outputting it in JSON format with FOSRestBundle and JMSSerializer. I've setup properties annotations for this entity to expose needed properties via serialization groups and it's working great.

但是我需要添加一些不属于实体本身的属性.例如,我希望我的pizza具有属性:isFresh,该属性由某些PizzaService::isFresh(Pizza $pizza)服务确定.我该怎么做?

But I need to add some properties that do not belong to the entity itself. For example I want my pizza to have property: isFresh that is determined by some PizzaService::isFresh(Pizza $pizza) service. How do I do this?

  • 我应该在序列化过程中注入一些其他逻辑(如果是的话)吗?
  • 我是否应该创建一个包装实体,使其具有要从原始实体中公开的属性以及其他 external 属性?
  • 是否应该将属性isFresh添加到原始的Pizza实体并在序列化之前填充到控制器中?
  • 是否应该返回独立于实体数据的其他数据(例如,在同级JSON属性中)?
  • Should I inject some additional logic to serialization process (if so how)?
  • Should I create a wrapper entity with properties that I want to expose from original entity plus additional external properties?
  • Should I add property isFresh to the original Pizza entity and populate in in the controller before serialization?
  • Should I return additional data independent of entity data (in a sibling JSON properties for example)?

换句话说:围绕此问题的最佳实践是什么?你能提供例子吗?谢谢.

In other words: what are the best practices around this issue? Could you provide examples? Thank you.

推荐答案

我决定创建自己的类以序列化实体.

I've decided to create my own class to serialize an entity.

这里是例子:

class PizzaSerializer implements ObjectSerializerInterface
{
    /** @var PizzaService */
    protected $pizzaService;

    /**
     * @param PizzaService $pizzaService
     */
    public function __construct(PizzaService $pizzaService)
    {
        $this->pizzaService = $pizzaService;
    }

    /**
     * @param Pizza $pizza
     * @return array
     */
    public function serialize(Pizza $pizza)
    {
        return [
            'id'      => $pizza->getId(),
            'size'    => $pizza->getSize(),
            'name'    => $pizza->getName(),
            'isFresh' => $this->pizzaService->isFresh($pizza),
        ];
    }
}

您只需要配置DC即可将PizzaService注入对象序列化器,然后从控制器中像这样调用它:

You just have to configure DC to inject PizzaService into the object serializer and then just call it like this from the controller:

$pizza = getPizzaFromSomewhere();

$pizzaSerializer = $this->get('serializer.pizza');

return $pizzaSerializer->serialize($pizza);

对象序列化程序将返回一个数组,该数组可以使用真实的序列化程序轻松转换为 JSON XML YAML 或任何其他格式就像JMS Serializer. FOSRestBundle会自动执行此操作.

The object serializer will return an array that can be easily converted to JSON, XML, YAML or any other format by using real serializer like JMS Serializer. FOSRestBundle will do this automatically if you configured it so.

这篇关于将自定义属性添加到序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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