Hi all,
I’ve developped an Event with LocalDate attribute. When testing it, I get the following exception:
com.thoughtworks.xstream.converters.ConversionException: Cannot convert type java.time.Ser to type java.time.LocalDate
---- Debugging information ----
class : org.axonlab.basics.jsr303.api.PersonCreatedEvent
required-type : org.axonlab.basics.jsr303.api.PersonCreatedEvent
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /org.axonlab.basics.jsr303.api.PersonCreatedEvent/dateBirth
line number : 1
version : 1.4.6
It seems that XStream is not able to convert Java8 LocalDate. I’ve not found any related additional converters and it doesn’t seem to included in XStream 1.4.7.
Do I miss ST? Did anyone already encountered the same issue ?
I too faced this in the last few weeks, and I have a solution for you!
You will need a LocalDateConverter :
public class LocalDateConverter extends AbstractSingleValueConverter {
public boolean canConvert(Class type) {
return (type!=null) && LocalDate.class.getPackage().equals(type.getPackage());
}
public String toString (Object source) {
return source.toString();
}
public Object fromString(String str) {
try {
return LocalDate.parse(str);
} catch (Exception e) {
// deal with it
}
}
}
When you wire up the EventStore (I do it in code) :
LocalDateConverter conv = new LocalDateConverter();
Xtream xstream = new XStream();
xstream.registerConverter(conv);
XStreamSerializer xstreamSerial = new XStreamSerializer (xstream);
EventStore store = new JpaEventStore (entityManagerProvider(), xstreamSerial);
Forgive typos please - I cannot paste work code externally, so retyped above.
I use obviously use JPA - so YMMV.
HTH!
Phil
You’re a boss thanks. I will test it!
This helped me get Java Instant support working with very similar converter code. It really helped to have the event store wiring example. Thanks.
Kevin