在JMS序列化器中排除空属性 [英] Exclude null properties in JMS Serializer
问题描述
我使用的XML API可以选择仅检索响应的一部分.
如果使用此功能,则导致生成的对象具有很多NULL
属性.
有没有一种方法实际上可以跳过NULL
属性?我尝试使用
My consumed XML API has an option to retrieve only parts of the response.
This causes the resulting object to have a lot of NULL
properties if this feature is used.
Is there a way to actually skip NULL
properties? I tried to implement an exclusion strategy with
shouldSkipProperty(PropertyMetadata $property, Context $context)`
但是我意识到无法访问当前属性值.
but i realized there is no way to access the current property value.
一个例子是下面的类
class Hotel {
/**
* @Type("string")
*/
public $id;
/**
* @Type("integer")
*/
public $bookable;
/**
* @Type("string")
*/
public $name;
/**
* @Type("integer")
*/
public $type;
/**
* @Type("double")
*/
public $stars;
/**
* @Type("MssPhp\Schema\Response\Address")
*/
public $address;
/**
* @Type("integer")
*/
public $themes;
/**
* @Type("integer")
*/
public $features;
/**
* @Type("MssPhp\Schema\Response\Location")
*/
public $location;
/**
* @Type("MssPhp\Schema\Response\Pos")
*/
public $pos;
/**
* @Type("integer")
*/
public $price_engine;
/**
* @Type("string")
*/
public $language;
/**
* @Type("integer")
*/
public $price_from;
}
在此特定的api调用中反序列化具有许多null
属性的以下对象.
which deserializes in this specific api call to the following object with a lot of null
properties.
"hotel": [
{
"id": "11230",
"bookable": 1,
"name": "Hotel Test",
"type": 1,
"stars": 3,
"address": null,
"themes": null,
"features": null,
"location": null,
"pos": null,
"price_engine": 0,
"language": "de",
"price_from": 56
}
]
但是我希望它成为
"hotel": [
{
"id": "11230",
"bookable": 1,
"name": "Hotel Test",
"type": 1,
"stars": 3,
"price_engine": 0,
"language": "de",
"price_from": 56
}
]
推荐答案
您可以将JMS序列化程序配置为跳过null
属性,如下所示:
You can configure JMS Serializer to skip null
properties like so:
$serializer = JMS\SerializerBuilder::create();
$serializedString = $serializer->serialize(
$data,
'xml',
JMS\SerializationContext::create()->setSerializeNull(true)
);
来自此问题.
更新:
不幸的是,如果您不希望在反序列化时使用空属性,那么别无选择,只能自己删除它们.
Unfortunately, if you don't want the empty properties when deserializing, there is no other way then removing them yourself.
但是,我不确定您实际想要删除这些属性的用例是什么,但是Hotel
类看起来并没有太多逻辑.在这种情况下,我想知道结果是否应该是一个类?
However, I'm not sure what your use case for actually wanting to remove these properties is, but it doesn't look like the Hotel
class contains much logic. In this case, I'm wondering whether the result has should be a class at all ?
我认为将数据表示为关联数组而不是对象会更自然.当然,JMS序列化器无法将数据反序列化为数组,因此您将需要一个数据传输对象.
I think it would be more natural to have the data represented as an associative array instead of an object. Of course, JMS Serializer cannot deserialize your data into an array, so you will need a data transfer object.
只需将dumpArray
和loadArray
方法添加到现有的Hotel
类中就足够了.这些将用于将数据转换为所需的结果,反之亦然.有您的DTO.
It's enough that you add dumpArray
and loadArray
methods to your existing Hotel
class. These will be used for transforming the data into your desired result and vice versa. There is your DTO.
/**
* Sets the object's properties based on the passed array
*/
public function loadArray(array $data)
{
}
/**
* Returns an associative array based on the objects properties
*/
public function dumpArray()
{
// filter out the properties that are empty here
}
我相信这是最干净的方法,它可能反映出您正在尝试做的更多事情.
I believe it's the cleanest approach and it might reflect what you're trying to do more.
我希望这会有所帮助.
这篇关于在JMS序列化器中排除空属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!