我正在处理XML文件的反序列化。某些元素可能无法包含任何数据,因此我尝试将以下XML元素(OfferDate
)反序列化为null
对象而不是{\DateTime
对象。 1}}对象:
<Product>
<OfferDate></OfferDate>
</Product>
...但我收到以下错误:
JMS\Serializer\Exception\RuntimeException: Invalid datetime "", expected format Y-m-d\TH:i:s.
./vendor/jms/serializer/src/JMS/Serializer/Handler/DateHandler.php:117
./vendor/jms/serializer/src/JMS/Serializer/Handler/DateHandler.php:99
./vendor/jms/serializer/src/JMS/Serializer/GraphNavigator.php:180
./vendor/jms/serializer/src/JMS/Serializer/XmlDeserializationVisitor.php:280
./vendor/jms/serializer/src/JMS/Serializer/GraphNavigator.php:236
./vendor/jms/serializer/src/JMS/Serializer/XmlDeserializationVisitor.php:175
./vendor/jms/serializer/src/JMS/Serializer/GraphNavigator.php:130
./vendor/jms/serializer/src/JMS/Serializer/XmlDeserializationVisitor.php:251
./vendor/jms/serializer/src/JMS/Serializer/GraphNavigator.php:236
./vendor/jms/serializer/src/JMS/Serializer/Serializer.php:182
./vendor/jms/serializer/src/JMS/Serializer/Serializer.php:116
./vendor/phpoption/phpoption/src/PhpOption/Some.php:89
./vendor/jms/serializer/src/JMS/Serializer/Serializer.php:119
./tests/AppBundle/Domain/Model/ProductTest.php:35
./tests/AppBundle/Domain/Model/ProductTest.php:44
如果XML文件在2016-09-25T18:58:55
中包含例如OfferDate
它会起作用,因为有一些数据......但是因为它也可能存在没有任何数据的元素数据我也要涉及这个案例。
我的YML映射将XML反序列化为对象:
AppBundle\Domain\Model\Product:
xml_root_name: Product
properties:
offerDate:
serialized_name: OfferDate
type: DateTime<'Y-m-d\TH:i:s'>
我的Product
课程:
<?php
declare(strict_types = 1);
namespace AppBundle\Domain\Model;
/**
* @author ...
*/
class Product
{
/**
* @var \DateTime
*/
private $offerDate;
/**
* @return \DateTime
*/
public function getOfferDate(): \DateTime
{
return $this->offerDate;
}
}
最后我的反序列化:
$xml = file_get_contents(__DIR__.'/product.xml');
$serializer = SerializerBuilder::create()
->addMetadataDir(__DIR__.'/../../../../app/config/serializer')
->build();
/** @var ProductCollection $productCollection */
$productCollection = $serializer->deserialize($xml, ProductCollection::class, 'xml');
$firstProduct = $productCollection->getProducts()[0];
var_dump($firstProduct->getOfferDate());
如上所示, ./tests/AppBundle/Domain/Model/ProductTest.php:35
错误等于行$productCollection = $serializer->deserialize($xml, ProductCollection::class, 'xml');
。
澄清为什么我反序列化为
ProductCollection
:product.xml
包含<Products>
个元素所在的<Product>
元素。然后ProductCollection
包含一个名为getProducts()
的方法,该方法返回一个包含反序列化Product
对象的数组。
有没有办法将OfferDate
元素(没有任何数据)反序列化为null
对象?如果是这样,怎么样?
答案 0 :(得分:0)
我已经为DateTime对象的反序列化过程创建了一个Handler。
这是我的解决方案。我的DateTimeHandler
覆盖了JMSSerializer提供的默认DateHandler
类和方法deserializeDateTimeFromXml
:
<?php
declare(strict_types = 1);
namespace AppBundle\Serializer\Handler;
use JMS\Serializer\Handler\DateHandler;
use JMS\Serializer\XmlDeserializationVisitor;
/**
* @author ...
*/
class DateTimeHandler extends DateHandler
{
/**
* @param XmlDeserializationVisitor $visitor
* @param $data
* @param array $type
*
* @return \DateTime|null
*/
public function deserializeDateTimeFromXml(XmlDeserializationVisitor $visitor, $data, array $type)
{
// Casting the data to a string will return the value of the
// current xml element. So if it's empty there is no data.
if ((string)$data === '') {
return null;
}
return parent::deserializeDateTimeFromXml($visitor, $data, $type);
}
}
然后在我的反序列化中:(注意configureHandlers
方法)
$xml = file_get_contents(__DIR__.'/product.xml');
$serializer = SerializerBuilder::create()
->addMetadataDir(__DIR__.'/../../../../app/config/serializer')
->configureHandlers(
function (HandlerRegistry $registry) {
$registry->registerSubscribingHandler(new DateTimeHandler());
}
)
->build();
/** @var ProductCollection $productCollection */
$productCollection = $serializer->deserialize($xml, ProductCollection::class, 'xml');
这现在完美无缺!