1

我正在使用 symfony 序列化程序。但是如果我安装作曲家包--no-dev标志的作曲家包,它会反序列化应该是数组数组中的对象数组的数据。

这是序列化:

$result = $this->get('serializer')->deserialize(
    $request->getContent(),
    InputDto::class,
    'json'
);

对于反序列化,我在 DTO 中使用注释。

这就是“字段”在 DTO 中对象数组的外观:

/**
 * @var OrderItemDto[]|Collection
 */
private $items = [];
4

2 回答 2

2

基于代码:

https://github.com/symfony/serializer/blob/master/Encoder/JsonDecode.php#L84

如果您将选项json_decode_associative传递为 false

$result = $this->get('serializer')->deserialize(
    $request->getContent(),
    InputDto::class,
    'json',
    ['json_decode_associative' => false]
);

它不应该尝试将其转换为数组。

http://php.net/manual/en/function.json-decode.php

于 2017-10-31T22:16:17.173 回答
1

要使对象子级别工作,您需要在下一行中app/config/config.yml 添加framework

property_info:
    enabled: true
于 2017-11-16T15:08:04.060 回答