serialize list of dates with Jackson
Asked Answered
A

2

7

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.

Apeldoorn answered 22/7, 2016 at 14:20 Comment(2)
What do you mean with annotations? You mean comments? Also, you can't serialise a list, in order to serialise a list, you need to put the data into a hashset, because hashsets are serialisable.Alnico
Something like this @JsonSerialize(using = JsonDateSerializer.class) on getter of executionDates variable.Apeldoorn
A
16

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).

Apeldoorn answered 22/7, 2016 at 15:23 Comment(1)
To everyone watching this: please consider to have consistency between private fields name and getter/setter method: for example if you put an annotation on a private field but its public getter method it's named differently, the final result won't use annotation featureJosphinejoss
A
0

Try something like this:

For serialising:

@Component
public class JsonDateSerializer extends JsonSerializer<Date>
{
    // ISO 8601
    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
            throws IOException, JsonProcessingException
    {
        String formattedDate = dateFormat.format(date);
        gen.writeString(formattedDate);
    }
}
Alnico answered 22/7, 2016 at 14:47 Comment(4)
I already tried but i get this exception: com.fasterxml.jackson.databind.JsonMappingException: java.util.Arrays$ArrayList cannot be cast to java.util.Date . Is not being applied to each element in List, but to entire collection.Apeldoorn
Do you have this somewhere in your code? @JsonSerialize(using=JsonDateSerializer.class) EDIT: Have you tried setting the date into a variable type Date, instead of putting the data into an array list?Alnico
Yes, i have like this: @JsonSerialize(using = JsonDateSerializer.class) public final List<Date> getExecutionDates() { return executionDates; }. With a variable type Date instead of List of Date's, works but i want a list of dates, not just one date...Apeldoorn
Ok, how about you try and take everything in the list, and iterate through it and for every piece of data, make that data into a Date variable, and then serialise it?Alnico

© 2022 - 2024 — McMap. All rights reserved.