Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to serialize empty array to empty XML element with JMS serializer

I am assembling an XML file based on an XSD that requires an XML container element to be present, even if it is empty.

When I try to serialize an empty array, using JMS serializer, with configuration that works if the array is not empty, I get no element at all.

Can I resolve this by configuration or will I have to implement my own event handler?

Thanks in advance.

like image 637
rhummelmose Avatar asked Mar 29 '17 09:03

rhummelmose


2 Answers

I did some digging and it turned out that there is an undocumented option that can be specified on xml_list, called skip_when_empty.

Support for the above mentioned property was also implemented in xsd2php with the following PR that was merged into master a few days ago: https://github.com/goetas-webservices/xsd2php/pull/27

like image 97
rhummelmose Avatar answered Oct 15 '22 10:10

rhummelmose


Try this:

$serializer = JMS\SerializerBuilder::create();   
$context = JMS\SerializationContext::create()->setSerializeNull(true);           
$serializedString = $serializer->serialize($data, 'xml', $context);

here setSerializeNull(true) will force property/properties to serialize even if it's null

like image 41
rajatsaurastri Avatar answered Oct 15 '22 11:10

rajatsaurastri