【问题标题】:Use Ignore attribute with Symfony Serializer使用 Symfony 序列化程序的忽略属性
【发布时间】:2022-08-11 07:51:33
【问题描述】:

在我的 Symfony 6 项目中,我使用 symfony Serializer 和这个配置。

# config/packages/framework.yaml

framework:

    serializer:
        name_converter: \'serializer.name_converter.camel_case_to_snake_case\'
        enable_annotations: true
        default_context:
            datetime_format: Y-m-d

    annotations:
        enabled: true

我期待 #[Ignore] 属性从 json 中排除字段。但它没有效果。

class Event {

    #[ORM\\Id]
    #[ORM\\GeneratedValue]
    #[ORM\\Column(type: \'integer\')]
    #[Ignore]
    private $id;

    ...
}

我还尝试根据attributes-groupsignoring-attributes 在控制器中定义序列化程序,但结果仍然相同。

    $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
    $serializer = new Serializer([new ObjectNormalizer($classMetadataFactory)],[new JsonEncoder()]);
    return new JsonResponse($serializer->serialize($events,\'json\'));

我错过了什么,或者为什么它不排除基于 Ignore 属性的字段? 是否可以只使用 xml 配置来激活 Ignore 属性?

\"datetime_format\" 更改输出日期时间格式,但 \"name_converter\" 也不会将字段名称从 camelCase 更改为 sanke_case。

我想原因可能是GetSetMethodNormalizerObjectNormalizer 相比具有更高的优先级,并且它不使用序列化器配置。

    标签: symfony


    【解决方案1】:

    你的配置是正确的,你可以使用这个命令来验证你的配置:
    php bin/console debug:config framework serializer

    如果您自己使用序列化程序,请在ObjectNormalizer 的第二个参数中设置nameConverter。见代码withoutContainer函数`

    但是如果你想使用 symfony 配置,你应该在容器中使用服务,所以使用这种风格参见代码 withContainer 函数。

    <?php
    
    namespace App\Controller;
    
    use App\Entity\Event;
    use Doctrine\Common\Annotations\AnnotationReader;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\JsonResponse;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\Routing\Annotation\Route;
    use Symfony\Component\Serializer\Encoder\JsonEncoder;
    use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
    use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
    use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
    use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
    use Symfony\Component\Serializer\Serializer;
    use Symfony\Component\Serializer\SerializerInterface;
    
    class TestController extends AbstractController
    {
        #[Route('/without-container')]
        public function withoutContainer(): Response
        {
    
            $event = new Event();
            $event->setName('Published Email');
            $event->setIsPublished(true);
            
            $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
            $normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
            $serializer = new Serializer([$normalizer],[new JsonEncoder()]);
    
            return new JsonResponse($serializer->serialize($event,'json'));
            //"{\"name\":\"Published Email\",\"is_published\":true}"
        }
    
        #[Route('/with-container', name: 'app_home')]
        public function withContainer(SerializerInterface $serializer): Response
        {
            $event = new Event();
            $event->setName('Published Email');
            $event->setIsPublished(true);
    
            return new JsonResponse($serializer->serialize($event,'json'));
            // "{\"name\":\"Published Email\",\"is_published\":true}"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 2016-02-03
      • 2013-04-07
      • 1970-01-01
      相关资源
      最近更新 更多