Graceful way to get a List<LocalDate>

565 views
Skip to first unread message

A. Rick Anderson

unread,
Sep 25, 2020, 12:35:30 PM9/25/20
to jackson-user
Using custom serializer/deserializer classes, I've figured out how to work with LocalDate, with different formats, and in such a way that none of the client classes have to worry about the specific structure of my JSON DAO's. 
But, I haven't been able to figure out how to get from an JSON array of LocalDate to a Java List.  A List<String> works great.  Not so lucky with List<LocalDate>.  Any suggestions?

   static public final DateTimeFormatter meetingTitleFormatter     = DateTimeFormatter.ofPattern("dd MMMM yyyy");
   static public final DateTimeFormatter meetingIDFormatter        = DateTimeFormatter.ofPattern("yyyyMMdd");
   static public final DateTimeFormatter meetingDateFormatter      = DateTimeFormatter.ofPattern("yyyy-MM-dd");
   static public final DateTimeFormatter meetingShortDateFormatter = DateTimeFormatter.ofPattern("yyyy MMM dd");
   static public final DateTimeFormatter meetingLongDateFormatter  = DateTimeFormatter.ofPattern("yyyy MMMM dd");


   @JsonProperty("StringDates")
   private List<LocalDate> PossibleMeetingDates = new ArrayList<>();
   @JsonDeserialize(using = MeetingShortDateDeserializer.class)
   @JsonSerialize(using = MeetingShortDateSerializer.class)
   @JsonProperty("MeetingShortDate")
   private LocalDate    meetingShortDate;
   @JsonDeserialize(using = MeetingIDDeserializer.class)
   @JsonSerialize(using = MeetingIDSerializer.class)
   @JsonProperty("MeetingID")
   private LocalDate    meetingID;
   @JsonDeserialize(using = MeetingDateDeserializer.class)
   @JsonSerialize(using = MeetingDateSerializer.class)
   @JsonProperty("MeetingDate")
   private LocalDate    meetingDate;
   @JsonDeserialize(using = MeetingTitleDeserializer.class)
   @JsonSerialize(using = MeetingTitleSerializer.class)
   @JsonProperty("MeetingTitle")
   private LocalDate    meetingTitle;

Tatu Saloranta

unread,
Sep 25, 2020, 12:39:23 PM9/25/20
to jackson-user
I think what you are looking for is

@JsonDeserialize(contentUsing = MeetingShortDateSerializer.class)
List<LocalDate> meetingShortDate;

since `using` refers to value itself, and to specify deserializer for
elements of a container you need to use `contentUsing` instead.

-+ Tatu +-

A. Rick Anderson

unread,
Sep 25, 2020, 5:25:37 PM9/25/20
to jackson-user
Thank-you! 
You are a saint!  Such an elegant answer.  and now I know the 'secret' !

Tatu Saloranta

unread,
Sep 25, 2020, 5:32:19 PM9/25/20
to jackson-user
Glad I was able to help here, share some knowledge :)

-+ Tatu +-

On Fri, Sep 25, 2020 at 2:25 PM A. Rick Anderson
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/636e47f5-e020-48b1-bdc4-a8278f16ebd8n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages