1

I need to serialize an array of dates (just that, not an object containing the array of dates!) and I want to be able to control how it is done:

$serializer = SerializerBuilder::create()->build();
$dates = array(new DateTime('1990-07-20'), new DateTime('2000-07-20'));
$serialized = $serializer->serialize($dates, 'xml');

Currently I get something like:

<result>
  <entry><![CDATA[1990-07-20T00:00:00+0000]]><entry/>
  <entry><![CDATA[2000-07-20T00:00:00+0000]]><entry/>
</result>

I want to configure things like: differnt tags, differnt format for the dates, get rid of the CDATA. And get something like this:

  <dates>
    <date>1990-07-20<date/>
    <date>2000-07-20<date/>
  </dates>

One option I am considering is creating a Dates object that contains the array. Then I can use annotations or even a custom handler to add format to the dates and also describe the xml tags. But I think there should be an easier way. I really don't want to create a wrapper object just to tell how an array of dates should be serialized. It is not a terrible solution though. But let me know if you think of a better one or answer the above question. Thanks!

2
  • Look here: jmsyst.com/libs/serializer/master/handlers It seems to be what you are looking for. Commented Dec 25, 2013 at 17:37
  • Hi Markus, thanks for answering. I'm trying to see if there is an easier way than wrapping my array of dates, or creating a handler. This question is related to this one: link. I want to serialize the array just redefining the node names and the date format. I've read the documentation of handlers and it is not clear to me how to change the tag names in an easy way. Commented Dec 25, 2013 at 19:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.