我有一个抽象的父(映射超级)类,它有几个具有不同属性的子节点,我想反序列化.我使用MongoDB和Doctrine ODM存储数据,因此我还有一个discriminator字段,它告诉doctrine使用了哪个子类(并且还有一个自定义的"type"属性ontop,用于确定当前处理的类).
在反序列化我的模型时,我得到一个异常,告诉我不可能创建一个抽象类的实例(ofcourse) - 现在我想知道如何告诉JMS反序列化器继承它应该使用的类(这就是我使用的原因)type例如一个自定义实例变量 - 因为我无权访问doctrine的鉴别器字段映射.
我可以成功地插入preDeserializeEvent- 所以也许可以在那里(或使用)制作一些开关/案例?
我的模型简称(抽象类):
<?php
namespace VBCMS\Bundle\AdminBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use JMS\Serializer\Annotation as Serializer;
/**
* abstract Class Module
* @Serializer\AccessType("public_method")
* @MongoDB\MappedSuperclass
* @MongoDB\InheritanceType("SINGLE_COLLECTION")
* @MongoDB\DiscriminatorField(fieldName="_discriminator_field")
* @MongoDB\DiscriminatorMap({
* "module"="Module",
* "text_module"="TextModule",
* "menu_module"="MenuModule",
* "image_module"="ImageModule"
* })
*/
abstract class Module {
const TYPE_MODULE_TEXT = 'module.text';
const TYPE_MODULE_MENU = 'module.menu';
const TYPE_MODULE_MEDIA_ITEM = 'module.media.item';
/**
* @Serializer\Type("string")
* @MongoDB\Field(type="string")
* @var String
*/
protected $type;
/**
* …Run Code Online (Sandbox Code Playgroud) 我正在尝试为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");
}
}
Run Code Online (Sandbox Code Playgroud)
这什么都不做,也不会死.这就是我注册处理程序的方式
$serializer = SerializerBuilder::create()
->configureHandlers(function(HandlerRegistry $registry) {
$registry->registerSubscribingHandler(new MyHandler());
})
->addDefaultHandlers()
->build();
$json = $serializer->serialize($obj, 'json');
Run Code Online (Sandbox Code Playgroud)
我的处理程序从未被调用过,我无法操纵序列化数据.
我试图在Symfony 2.1中序列化带有嵌入文档的MongoDB文档.我正在使用JMSserializer和Mongodb-odm包.
我有以下文件实体.
// Blog
namespace App\DocumentBundle\Document;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use JMS\SerializerBundle\Annotation\Type;
/**
* @MongoDB\Document(repositoryClass="App\DocumentBundle\Repository\BlogRepository")
*/
class Blog {
/**
* @MongoDB\Id
*/
protected $id;
/**
* @MongoDB\String
* @Assert\NotBlank()
*/
protected $title;
/**
* @MongoDB\string
* @Assert\NotBlank()
*/
protected $blog;
/**
* @MongoDB\EmbedMany(targetDocument="Tag")
*/
private $tags;
/**
* @MongoDB\Timestamp
*/
protected $created;
/**
* @MongoDB\Timestamp
*/
protected $updated;
}
Run Code Online (Sandbox Code Playgroud)
和
// Tag
namespace App\DocumentBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\EmbeddedDocument
*/
class Tag …Run Code Online (Sandbox Code Playgroud) 正如标题所说,我正在尝试做出是否在序列化中包含字段的运行时决定.就我而言,这个决定将基于权限.
我正在使用Symfony 2,所以我要做的是添加一个名为@ExcludeIf的附加注释,它接受一个安全表达式.
我可以处理元数据的注释解析和存储,但我无法看到如何将自定义排除策略与库集成.
有什么建议?
注意:排除策略是JMS代码库中的实际构造,我只是无法找出在其他代码库之上集成额外内容的最佳方法
PS:之前我曾经问过这个问题,并指出要使用小组.由于各种原因,这对我的需求来说是一个非常差的解决方案
我正在使用JMSSerializerBundle来序列化我的实体.但我有以下问题:属性名称是"className"但在我的Json对象中我得到一个"class_name".
这是我的实体:
/**
* Events
*
* @ORM\Table()
* @ORM\Entity
*/
class Events
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
/**
* @var string
*
* @ORM\Column(name="className", type="string", length=255)
*/
private $className;
/**
* Set className
*
* @param string $className
* @return Events
*/
public function setClassName($className)
{
$this->className = $className;
return $this;
}
/**
* Get className
*
* @return string
*/
public function getClassName()
{
return $this->className; …Run Code Online (Sandbox Code Playgroud) 我的配置是
jms_serializer:
metadata:
auto_detection: true
directories:
NameOfBundle:
namespace_prefix: ""
path: "@VendorNameOfBundle/Resources/config/serializer"
Run Code Online (Sandbox Code Playgroud)
我的YML文件名为Entity.Project.ymlcontains
Vendor\NameOfBundle\Entity\Project:
exclusion_policy: ALL
properties:
id:
expose: true
Run Code Online (Sandbox Code Playgroud)
我正在从Controller中加载序列化器
$serializer = SerializerBuilder::create()
->configureListeners(function(EventDispatcher $dispatcher) {
$dispatcher->addSubscriber(new ProjectSubscriber($this->container));
})
->addDefaultListeners()
->build();
Run Code Online (Sandbox Code Playgroud)
这完全忽略了我的YML文件并公开了Project中的所有字段.我已经清除了缓存.
但是如果我在没有自定义订阅者的情况下使用它,那么排除工作就可以了
$serializer = $this->get("jms_serializer");
Run Code Online (Sandbox Code Playgroud)
即使明确添加目录也不起作用
$serializer = SerializerBuilder::create()
->configureListeners(function(EventDispatcher $dispatcher) {
$dispatcher->addSubscriber(new ProjectSubscriber($this->container));
})
->addDefaultListeners()
->addMetadataDir(realpath($this->get('kernel')->getRootDir()."/../") . '/src/Vendor/NameOfBundle/Resources/config/serializer')
->build();
Run Code Online (Sandbox Code Playgroud)
关于如何定义这条路径的文档并不清楚.上面的方法没有错误,但没有拉入YML文件.以下方法错误并说该目录不存在;
$serializer = SerializerBuilder::create()
->configureListeners(function(EventDispatcher $dispatcher) {
$dispatcher->addSubscriber(new ProjectSubscriber($this->container));
})
->addDefaultListeners()
->addMetadataDir('@VendorNameOfBundle/Resources/config/serializer')
->build();
Run Code Online (Sandbox Code Playgroud)
如何让JMS Serializer查看我的YML文件以排除字段并使用订阅服务器?
为什么除了json中的数据之外的所有值都使用null实例化新实体,为什么实体构造函数没有设置默认值 - 在构造函数中放置die()永远不会被执行.
好的,深入研究代码,当没有找到托管实体时,JMSS将使用doctrine instantiator类创建实体 - 它唯一的工作,创建实体而不调用构造函数.是否有一个原因?这是在里面JMS\Serializer\Construction\UnserializeObjectConstructor
我已经将对象构造函数配置为使用JMS编写的doctrine对象构造函数,但是同样的问题在有和没有这个的情况下发生.
jms_serializer.object_constructor:
alias: jms_serializer.doctrine_object_constructor
public: false
Run Code Online (Sandbox Code Playgroud)
现有实体更新没有问题,但是新实体缺少所有构造函数集默认值.
在'fields'元素0存在下,元素1是新的.
array (size=3)
'id' => int 2
'name' => string 'Categories' (length=10)
'fields' =>
array (size=2)
0 =>
array (size=7)
'id' => int 49
'displayName' => string 'Car Branded' (length=11)
'type' => string 'checkboxlist' (length=12)
'required' => boolean false
'disabled' => boolean false
'name' => string 'h49' (length=3)
1 =>
array (size=3)
'type' => string 'email' (length=5)
'name' => string 'field3491' (length=9)
'displayName' => …Run Code Online (Sandbox Code Playgroud) 我正在使用FOS Rest bundle和JMS Serializer来创建REST Api.问题是我想保留JSON响应中的属性名称,而不是使用_.
例如,我有一个名为employeeIdentifier的属性,默认情况下会转换为employee_identifier.
我看到配置中有一个选项可以禁用小写并删除_,但随后它变为EmployeeIdentifier.
有没有什么方法JMS Serializer保留属性的原始名称?提前致谢
use JMS\Serializer\SerializationContext;
$context = SerializationContext::create()->setGroups(array(
'Default', // Serialize John's name
'manager_group', // Serialize John's manager
'friends_group', // Serialize John's friends
'manager' => array( // Override the groups for the manager of John
'Default', // Serialize John manager's name
'friends_group', // Serialize John manager's friends. If you do not override the groups for the friends, it will default to Default.
),
'friends' => array( // Override the groups for the friends of John
'manager_group' // Serialize …Run Code Online (Sandbox Code Playgroud) 我们使用Symfony2 FOSRestBundle和JMSSerializerBundle来开发移动开发人员使用的REST API.
JSON格式的API响应在适用的情况下返回"null"作为属性的值,这将为移动开发人员使用的第三方库生成例外.
我没有看到JMSSerializerBundle或FOSRestBundle的解决方案根据我们的要求覆盖该值.
到目前为止的解决方法 我可以在实体中设置默认值,以便新数据在数据库中具有一些默认值,而不是null.但这对于一对一/多对一关系对象不起作用,因为默认情况下它们将返回null而不是空白对象.
在序列化后覆盖json的任何解决方案?