Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serialize list of dates with Jackson

I'm trying to serialize an Object that contains a List of Dates and i want to serialize to a JSON list of dates (String) in a specific format (yyyy-MM-dd).

private List<Date> executionDates;

will become like:

"executionDates": [
  "2016-07-22",
  "2016-07-23",
  "2016-07-24"
]

It is possible to do it with annotations?

Thanks in advance.

like image 782
lealoureiro Avatar asked Jul 22 '16 14:07

lealoureiro


1 Answers

I found the solution. I had to use property contentUsing instead of using in annotation like this:

@JsonSerialize(contentUsing = JsonDateSerializer.class)

contentUsing property is used for collections. From class documentation:

Serializer class to use for serializing contents (elements of a Collection/array, values of Maps) of annotated property. Can only be used on properties (methods, fields, constructors), and not value classes themselves (as they are typically generic).

like image 133
lealoureiro Avatar answered Oct 06 '22 18:10

lealoureiro