Unable to serialize empty array to empty XML element with JMS serializer
Asked Answered
H

2

9

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.

Handiness answered 29/3, 2017 at 9:13 Comment(1)
How do you init / call the Serializer?Addictive
H
3

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

Handiness answered 5/4, 2017 at 20:7 Comment(0)
M
1

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

Modred answered 4/4, 2017 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.