使用 Symfony 2 序列化程序对对象中的嵌套结构进行非规范化 [英] Denormalize nested structure in objects with Symfony 2 serializer

查看:26
本文介绍了使用 Symfony 2 序列化程序对对象中的嵌套结构进行非规范化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 2.8 版的 Symfony 2 项目,并且我正在使用内置组件 Serializer ->http://symfony.com/doc/current/components/serializer.html

I'm working on a Symfony 2 project with version 2.8 and I'm using the build-in component Serializer -> http://symfony.com/doc/current/components/serializer.html

我有一个由 Web 服务提供的 JSON 结构.反序列化后,我想对对象中的内容进行非规范化.这是我的结构(汽车应用程序上下文中的模型/制造).

I have a JSON structure provided by a web service. After deserialization, I want to denormalize my content in objects. Here is my structure (model/make in a car application context).

[{
"0": {
    "id": 0,
    "code": 1,
    "model": "modelA",
    "make": {
        "id": 0,
        "code": 1,
        "name": "makeA"
    }
  }
} , {
 "1": {
    "id": 1,
    "code": 2,
    "model": "modelB",
    "make": {
        "id": 0,
        "code": 1,
        "name": "makeA"
    }
  }
}]

我的想法是填充一个 VehicleModel 对象,其中包含对 VehicleMake 对象的引用.

My idea is to populate a VehicleModel object which contains a reference to a VehicleMake object.

class VehicleModel {
    public $id;
    public $code;
    public $model;
    public $make; // VehicleMake
}

这是我所做的:

// Retrieve data in JSON
$data = ...
$serializer = new Serializer([new ObjectNormalizer(), new ArrayDenormalizer()], [new JsonEncoder()]);
$models = $serializer->deserialize($data, 'NamespaceVehicleModel[]', 'json');

结果,我的对象 VehicleModel 被正确填充,但 $make 在逻辑上是一个键/值数组.在这里,我想要一个 VehicleMake 代替.

In result, my object VehicleModel is correctly populated but $make is logically a key/value array. Here I want a VehicleMake instead.

有没有办法做到这一点?

Is there a way to do that?

推荐答案

ObjectNormalizer 需要更多配置.您至少需要提供 PropertyTypeExtractorInterface 类型的第四个参数.

The ObjectNormalizer needs more configuration. You will at least need to supply the fourth parameter of type PropertyTypeExtractorInterface.

这是一个(相当笨拙的)例子:

Here's a (rather hacky) example:

<?php
use SymfonyComponentPropertyInfoPropertyTypeExtractorInterface;
use SymfonyComponentPropertyInfoType;
use SymfonyComponentSerializerEncoderJsonEncoder;
use SymfonyComponentSerializerNormalizerArrayDenormalizer;
use SymfonyComponentSerializerNormalizerObjectNormalizer;
use SymfonyComponentSerializerSerializer;

$a = new VehicleModel();
$a->id = 0;
$a->code = 1;
$a->model = 'modalA';
$a->make = new VehicleMake();
$a->make->id = 0;
$a->make->code = 1;
$a->make->name = 'makeA';

$b = new VehicleModel();
$b->id = 1;
$b->code = 2;
$b->model = 'modelB';
$b->make = new VehicleMake();
$b->make->id = 0;
$b->make->code = 1;
$b->make->name = 'makeA';

$data = [$a, $b];

$serializer = new Serializer(
    [new ObjectNormalizer(null, null, null, new class implements PropertyTypeExtractorInterface {
        /**
         * {@inheritdoc}
         */
        public function getTypes($class, $property, array $context = array())
        {
            if (!is_a($class, VehicleModel::class, true)) {
                return null;
            }

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

            return [
                new Type(Type::BUILTIN_TYPE_OBJECT, true, VehicleMake::class)
            ];
        }
    }), new ArrayDenormalizer()],
    [new JsonEncoder()]
);

$json = $serializer->serialize($data, 'json');
print_r($json);

$models = $serializer->deserialize($json, VehicleModel::class . '[]', 'json');
print_r($models);

<小时>

请注意,在您的示例 json 中,第一个条目有一个数组作为 make 的值.我认为这是一个错字,如果是故意的,请发表评论.


Note that in your example json, the first entry has an array as value for make. I took this to be a typo, if it's deliberate, please leave a comment.

为了使更加自动化,您可能需要尝试使用 PhpDocExtractor.

To make this more automatic you might want to experiment with the PhpDocExtractor.

这篇关于使用 Symfony 2 序列化程序对对象中的嵌套结构进行非规范化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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