如何在 Symfony 中配置 JMSSerializer 来序列化自定义 class to/from int?

How configure JMSSerializer in Symfony to serialize custom class to/from int?

我正在开发一个基于 Symfony 3.4 的 Web 应用程序项目,该项目使用 JMSSerializer 将不同的自定义 class 序列化为 JSON 以将此数据发送到移动应用程序。

如何 serialize/deserialize 自定义 class to/from 到 int?


假设我们有以下 classes:

<?php

// AppBundle/Entity/...

class NotificationInfo {
    public $date;      // DateTime
    public $priority;  // Int 1-10
    public $repeates;  // Boolean

    public function toInt() {
        // Create a simple Int value
        //  date = 04/27/2020
        //  priority = 5
        //  repeats = true
        //  ==> int value = 4272020 5 1 = 427202051
    }

    public function __construnct($intValue) {
       // ==> Split int value into date, priority and repeats...
    }
}


class ToDoItem {
    public $title;
    public $tags;
    public $notificationInfo;
}


// AppBundle/Resources/config/serializer/Entiy.ToDoItem.yml
AppBundle\Entity\ToDoItem:
exclusion_policy: ALL
properties:
    title:
        type: string
        expose: true
    tags:
        type: string
        expose: true
    notificationInfo:
        type: integer
        expose: true

因此 class NotificationInfo 也具有从 int 创建它并将其序列化为 in 的功能。如何告诉序列化器它应该将 $notificationInfo 的值序列化为整数?

我可以改用以下内容:

    notificationInfo:
        type: AppBundle\Entity\NotificationInfo
        expose: true

但是在这种情况下,我需要配置 NotificationInfo 的序列化,我只能指定哪个 属性 应该序列化为哪个值...


编辑:

这是我要创建的JSON:

{
    "title": "Something ToDO",
    "tags": "some,tags",
    "notificationInfo": 427202051
}

这不是我要找的:

{
    "title": "Something ToDO",
    "tags": "some,tags",
    "notificationInfo": {
        "intValue": 427202051
    }
}

you can use VirtualProperty method to add any method of you class into json response

use JMS\Serializer\Annotation as Serializer;

class NotificationInfo 
{
    /**
     * @return int
     * @Serializer\VirtualProperty()
     * @Serializer\SerializedName("formatedLocation")
     */
    public function toInt()
    {
        return 4272020;
    }
}

经过更多的挖掘,我找到了以下解决方案来解决我的问题:我添加了一个自定义序列化 Handler,它告诉 JMSSerializer 如何处理我的自定义 class:

class NotificationInfoHandler implements SubscribingHandlerInterface {

    public static function getSubscribingMethods() {
        return [
            [
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
                'format' => 'json',
                'type' => 'NotificationInfo',
                'method' => 'serializeNotificationInfoToJson',
            ],
            [
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
                'format' => 'json',
                'type' => 'NotificationInfo',
                'method' => 'deserializeNotificationInfoToJson',
            ],
        ;


    public function serializeNotificationInfoToJson(JsonSerializationVisitor $visitor, NotificationInfo $info, array $type, Context $context) {
        return $info->toInt();
    }

    public function deserializeNotificationInfoToJson(JsonDeserializationVisitor $visitor, $infoAsInt, array $type, Context $context) {
        return (is_int($infoAsInt) ? NotificationInfo::fromInt($infoAsInt) : NotificationInfo::emptyInfo());
    }

}

感谢 autowire 处理程序自动添加并可用于序列化程序元数据:

notificationInfo:
    type: NotificationInfo
    expose: true