如何在Symfony中配置JMSSerializer以将自定义类序列化到int或从int序列化?
我正在从事一个基于Symfony 3.4的web应用程序项目,该项目使用如何在Symfony中配置JMSSerializer以将自定义类序列化到int或从int序列化?,symfony,jms-serializer,Symfony,Jms Serializer,我正在从事一个基于Symfony 3.4的web应用程序项目,该项目使用JMSSerializer将不同的自定义类序列化为JSON,以将这些数据发送到移动应用程序 如何将自定义类序列化/反序列化为int/from-to-int? 假设我们有以下类别: <?php // AppBundle/Entity/... class NotificationInfo { public $date; // DateTime public $priority; // In
JMSSerializer
将不同的自定义类序列化为JSON,以将这些数据发送到移动应用程序
如何将自定义类序列化/反序列化为int/from-to-int?
假设我们有以下类别:
<?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
但是在这种情况下,我需要配置NotificationInfo
的序列化,在这里我只能指定哪个属性应该序列化为哪个值
编辑: 这是我想要创建的JSON:
{
"title": "Something ToDO",
"tags": "some,tags",
"notificationInfo": 427202051
}
这不是我想要的:
{
"title": "Something ToDO",
"tags": "some,tags",
"notificationInfo": {
"intValue": 427202051
}
}
您可以使用VirtualProperty
方法添加类的任何方法
转换为json响应
在深入挖掘之后,我找到了以下解决方案:我添加了一个自定义序列化
处理程序
,它告诉JMSSerializer
如何处理我的自定义类:
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
您尝试过JMS序列化程序的VirtualProperty()方法吗?谢谢,但是向
NotificationInfo
添加虚拟属性只会更改序列化对象的内容。但我希望将完整的NotificationInfo
object/property序列化为一个int。我编辑了这个问题以演示其区别。
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());
}
}
notificationInfo:
type: NotificationInfo
expose: true