Serialization symfony反序列化嵌套对象
我已经使用Symfony序列化程序来序列化我的Serialization symfony反序列化嵌套对象,serialization,deserialization,symfony-3.4,Serialization,Deserialization,Symfony 3.4,我已经使用Symfony序列化程序来序列化我的Recherche对象。 在Recherche对象中,我有子对象:category和liue 当我反序列化我的Recherche对象时,所有子对象都在数组中转换。我希望它们再次成为物体 以下是我序列化对象的方式: $encoders=array(新的JsonEncoder()); $normalizer=新的ObjectNormalizer(); $normalizer->setIgnoredAttributes(数组('parent','enfan
Recherche
对象。
在Recherche
对象中,我有子对象:category
和liue
当我反序列化我的Recherche
对象时,所有子对象都在数组中转换。我希望它们再次成为物体
以下是我序列化对象的方式:
$encoders=array(新的JsonEncoder());
$normalizer=新的ObjectNormalizer();
$normalizer->setIgnoredAttributes(数组('parent','enfants');
$normalizer->setCircularReferenceHandler(函数($object){
返回$object->getCode();
});
$normalizer=数组($normalizer);
$serializer=新的序列化程序($normalizers,$encoders);
$rechercheJson=$serializer->serialize($recherche,'json');
我就是这样反序列化它的:
$encoders=array(新的JsonEncoder());
$normalizer=新的ObjectNormalizer();
$normalizer->setIgnoredAttributes(数组('parent','enfants');
$normalizer->setCircularReferenceHandler(函数($object){
返回$object->getCode();
});
$normalizer=数组($normalizer);
$serializer=新的序列化程序($normalizers,$encoders);
$recherche=$serializer->反序列化($recherche_json,recherche::class,'json');
我想可能与normalizer有关,但我在文档中找不到任何对我有帮助的东西
有人想帮忙吗
谢谢
编辑:
看到这篇文章后:
我试过这个:
$encoders=array(新的JsonEncoder());
$normalizer=newObjectNormalizer(null,null,null,new-SerializationPropertyTypeExtractor());
$normalizer->setIgnoredAttributes(数组('parent','enfants');
$normalizer->setCircularReferenceHandler(函数($object){
返回$object->getCode();
});
$normalizers=数组($normalizer,new arraydennormalizer());
$serializer=新的序列化程序($normalizers,$encoders);
$recherche=$serializer->反序列化($recherche_json,recherche::class,'json');
以及SerializationPropertyTypeExtractor:
class SerializationPropertyTypeExtractor实现PropertyTypeExtractor接口{
/**
*{@inheritardoc}
*/
公共函数getTypes($class,$property,array$context=array())
{
如果(!是a($class,Recherche::class,true)){
返回null;
}
如果('make'!=$property){
返回null;
}
如果('liue'==$property)
{
返回[new Type(Type::BUILTIN_Type_OBJECT,true,LieuRecherche::class)];
}
如果('categorie'==$property)
{
返回[new Type(Type::BUILTIN_Type_OBJECT,true,Categorie::class)];
}
返回null;
}
}
这很有效 参考:我的评论。
我知道您正在以经典的方式使用序列化程序,但我建议您应用推荐的策略模式,这样您就可以完全控制组件,并且可以轻松地进行扩展
我们的想法是为规范化和非规范化创建单独的类,如下所示:
/src/反规范化器
或者,如果您想使用序列化程序(注意,我们不需要json_解码):
当然,在反规范化程序中,可以将数组转换为普通的旧php对象(实体)。或者只是输出一个数组,选择权在您
这样,您只需将SerializerInterface注入控制器,如下所示:
//denormalize
$this->denormalizer->denormalize(
json_decode($response->getContent(), true),
ConfigDenormalizer::class
);
//normalize
$form = $this->formFactory->create(myConfigFormType::class);
$form->submit($data);
$this->normalizer->normalize($form);
use Symfony\Component\Serializer\SerializerInterface;
class EmbedController extends Controller
{
/**
* @var SerializerInterface
*/
private $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
}
这种方法也使单元测试变得更加容易,因为一切都是解耦的,因此可以进行模拟。:)
还值得查看以下文档:
特别是顶部的图像,它是一个很好的内存提示,提示您应该以何种方式使用每个类
希望这能有所帮助。我遇到了类似的问题,并尝试使用自定义PropertyTypeExtractor解决该问题。 因为我有很多实体都有嵌套对象,这感觉很麻烦,而且当嵌套对象再次嵌套对象时,它也不起作用 我使用PhpDocExtractor和ReflectionExtractor找到了一个更好的解决方案,它可以为您提取属性信息
$encoder = [new JsonEncoder()];
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
$normalizer = [new ArrayDenormalizer(), new ObjectNormalizer(null, null, null, $extractor)];
$serializer = new Serializer($normalizer, $encoder);
$result = $serializer->deserialize($data,someEntity::class,'json');
这是我的全部工作。我希望这会对某些人有所帮助。我通常使用symfony的序列化程序组件的策略模式。它允许您精确定义规范化和非规范化对象/数组的外观。稍后我将发送一个示例。是的,一个示例可能很好,因为我不理解:-)如果您使用自动连接,您不需要将(反)规范化器定义为服务或标记它们,这应该是现成的。只要您完成了
编写器需要序列化程序
;)谢谢你,爱德华。如果我能理解的话,你几乎所有的工作都是自己做的,对吗?Symfony序列化程序几乎毫无用处,不是吗?这取决于你,如果你想在没有自定义(反)规范化程序的情况下进行序列化或反序列化,你可以直接使用序列化程序服务$json=$this->serializer->serialize($object,'json')代码>但是在你的问题中,你想要一个自定义结构,要做到这一点,你需要自定义(反)规范化器。我找到了如何做到这一点。正如您在我问题的编辑部分所看到的,我已经测试了一个解决方案,它似乎不起作用,但最终效果很好。谢谢你的帮助
//deserialize
$array = $this->serializer->deserialize(
$response->getContent(),
ConfigDenormalizer::class,
'json'
);
//serialize
$json = $this->serialize->serialize($form, 'json');
use Symfony\Component\Serializer\SerializerInterface;
class EmbedController extends Controller
{
/**
* @var SerializerInterface
*/
private $serializer;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
}
$encoder = [new JsonEncoder()];
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
$normalizer = [new ArrayDenormalizer(), new ObjectNormalizer(null, null, null, $extractor)];
$serializer = new Serializer($normalizer, $encoder);
$result = $serializer->deserialize($data,someEntity::class,'json');