我有一个表示表、行和单元格的数据结构。我需要将其序列化和反序列化为JSON和XML。如何配置JMS序列化程序来序列化这种对象,
class Test {
public $data = [
'row_one' => ['cell-11', 'cell-12'],
'row_two' => ['cell-21', 'cell-22'],
];
}添加到这个XML中:
<result>
<data>
<row index="row_one">
<cell>cell-11</cell>
<cell>cell-12</cell>
</row>
<row index="row_two">
<cell>cell-21</cell>
<cell>cell-22</cell>
</row>
</data>
</result>我已经尝试使用对象来表示带有@Inline注释的行,它不适用于JSON中的反序列化,行是空的。
发布于 2015-10-29 05:00:14
最后,我设法对每个Row使用一个单独的对象来(反)序列化我的对象。
我的映射如下所示:
class Table
{
/**
* @\JMS\Serializer\Annotation\Inline
*/
private $rows = [];
public function __construct(array $rows)
{
$this->rows = $rows;
}
}
class Row
{
/**
* @\JMS\Serializer\Annotation\Inline
*/
private $values = [];
public function __construct(array $values)
{
$this->values = $values;
}
}然后我使用JMS序列化程序序列化我的对象:
echo $serializer->serialize(new Table([
new Row(['cell-11', 'cell-12']),
new Row(['cell-21', 'cell-22'])
]), 'json');不幸的是,由于我试图在这里修复的错误,使用这种方法反序列化不起作用-- https://github.com/schmittjoh/serializer/pull/513
https://stackoverflow.com/questions/33397911
复制相似问题