symfony反序列化嵌套对象 [英] symfony deserialize nested objects

查看:78
本文介绍了symfony反序列化嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Symfony序列化程序序列化了我的 Recherche 对象。
Recherche 对象中,我有子对象: Categorie Lieu



当我反序列化 Recherche 对象时,所有子对象都将转换为数组。我希望它们再次成为对象。



这是我序列化对象的方式:

  $ encoders = array(new JsonEncoder()); 
$ normalizer = new ObjectNormalizer();
$ normalizer-> setIgnoredAttributes(array('parent','enfants'));
$ normalizer-> setCircularReferenceHandler(function($ object){
return $ object-&get; getCode();
});
$ normalizers = array($ normalizer);
$ serializer =新的Serializer($ normalizers,$ encoders);
$ rechercheJson = $ serializer-> serialize($ recherche,‘json’);

这就是我反序列化的方式:



< pre class = lang-php prettyprint-override> $ encoders = array(new JsonEncoder());
$ normalizer = new ObjectNormalizer();
$ normalizer-> setIgnoredAttributes(array('parent','enfants'));
$ normalizer-> setCircularReferenceHandler(function($ object){
return $ object-> getCode();
});
$ normalizers = array($ normalizer);
$ serializer =新的Serializer($ normalizers,$ encoders);
$ recherche = $ serializer-> deserialize($ recherche_json,Recherche :: class,‘json’);

我认为也许与规范化有关,但是我找不到任何可以帮助我的东西在文档中。



任何人都有帮助的想法吗?



谢谢!



编辑:
看到此帖子后:使用symfony 2序列化程序反序列化对象中的嵌套结构



我尝试过:

  $ encoders = array(new JsonEncoder()); 
$ normalizer =新的ObjectNormalizer(null,null,null,新的SerializationPropertyTypeExtractor());
$ normalizer-> setIgnoredAttributes(array('parent','enfants'));
$ normalizer-> setCircularReferenceHandler(function($ object){
return $ object-&get; getCode();
});
$ normalizers = array($ normalizer,new ArrayDenormalizer());
$ serializer =新的Serializer($ normalizers,$ encoders);
$ recherche = $ serializer-> deserialize($ recherche_json,Recherche :: class,‘json’);

和SerializationPropertyTypeExtractor:

 类SerializationPropertyTypeExtractor实现PropertyTypeExtractorInterface {
/ **
* {@inheritdoc}
* /
公共函数getTypes($ class,$ property,array $ context = array())
{
if(!is_a($ class,Recherche :: class,true)){
返回null;
}

if('make'!== $ property){
返回null;
}

if('lieu'=== $ property)
{
return [new Type(Type :: BUILTIN_TYPE_OBJECT,true,LieuRecherche :: class) ];
}
if('categorie'=== $ property)
{
return [new Type(Type :: BUILTIN_TYPE_OBJECT,true,Categorie :: class)]];
}

返回null;
}
}

这很好!

解决方案

我遇到了类似的问题,并尝试使用自定义的PropertyTypeExtractor解决该问题。
因为我有很多带有嵌套对象的实体,所以当嵌套对象再次嵌套对象时,这感觉也很麻烦。



我发现一个更好的方法

  $ encoder = [new JsonEncoder()];使用PhpDocExtractor和ReflectionExtractor解决方案,提取您的财产信息。


$ extractor = new PropertyInfoExtractor([],[new PhpDocExtractor(),new ReflectionExtractor()]);
$ normalizer = [new ArrayDenormalizer(),new ObjectNormalizer(null,null,null,$ extractor)];
$ serializer =新的Serializer($ normalizer,$ encoder);
$ result = $ serializer->反序列化($ data,someEntity :: class,’json’);

这为我完成了所有工作。我希望这会对某人有所帮助。


I have used the Symfony serializer to serialize my Recherche object. In a Recherche object, I have sub objects : Categorie and Lieu.

When I deserialize my Recherche object, all the sub objects are transformed in arrays. I would like them to be objects again.

This is how I have serialized my object:

$encoders = array(new JsonEncoder());
$normalizer = new ObjectNormalizer();
$normalizer->setIgnoredAttributes(array('parent', 'enfants'));
$normalizer->setCircularReferenceHandler(function ($object) {
    return $object->getCode();
});
$normalizers = array($normalizer);
$serializer = new Serializer($normalizers, $encoders);
$rechercheJson= $serializer->serialize($recherche, 'json');

And this is how I deserialize it :

$encoders = array(new JsonEncoder());
$normalizer = new ObjectNormalizer();
$normalizer->setIgnoredAttributes(array('parent', 'enfants'));
$normalizer->setCircularReferenceHandler(function ($object) {
    return $object->getCode();
});
$normalizers = array($normalizer);
$serializer = new Serializer($normalizers, $encoders);
$recherche = $serializer->deserialize($recherche_json, Recherche::class, 'json');

I think maybe there is something to do with normalizer, but I can't find anything that helps me in the docs.

Anyone has an idea to help ?

Thanks !

EDIT : After seeing this post : Denormalize nested structure in objects with symfony 2 serializer

I tried this :

$encoders = array(new JsonEncoder());
            $normalizer = new ObjectNormalizer(null, null, null, new SerializationPropertyTypeExtractor());
            $normalizer->setIgnoredAttributes(array('parent', 'enfants'));
            $normalizer->setCircularReferenceHandler(function ($object) {
                return $object->getCode();
            });
            $normalizers = array($normalizer,  new ArrayDenormalizer());
            $serializer = new Serializer($normalizers, $encoders);
            $recherche = $serializer->deserialize($recherche_json, Recherche::class, 'json');

And the SerializationPropertyTypeExtractor:

class SerializationPropertyTypeExtractor implements PropertyTypeExtractorInterface {
    /**
     * {@inheritdoc}
     */
    public function getTypes($class, $property, array $context = array())
    {
        if (!is_a($class, Recherche::class, true)) {
            return null;
        }

        if ('make' !== $property) {
            return null;
        }

        if ('lieu' === $property)
        {
            return [new Type(Type::BUILTIN_TYPE_OBJECT, true, LieuRecherche::class)];
        }
        if ('categorie' === $property)
        {
            return [new Type(Type::BUILTIN_TYPE_OBJECT, true, Categorie::class)];
        }

        return null;
    }
}

And this works well !

解决方案

I had a similar issue and tried to solve the problem with a custom PropertyTypeExtractor. Since I have many Entities with nested Objects this felt quite cumbersome also it doesn't work when the nested Object has nested Object again.

I found a better solution using the PhpDocExtractor and the ReflectionExtractor, which extracts the property info for you.

$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');

This does all the work for me. I hope this will help someone.

这篇关于symfony反序列化嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆