I try to implement kryo into an existing project.
My project has a class "Calender", with an iCal4j object:
public class Calendar implements Serializable{
private static final long serialVersionUID = 1L;
private net.fortuna.ical4j.model.Calendar calendar;
private VTimeZone timezone;
/*
* constructor
*/
public Calendar() {
super();
//Create a TimeZone
TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
this.timezone = registry.getTimeZone("Europe/Berlin").getVTimeZone();
// Create a calendar
this.calendar = createCalendar();
}
kryo code:
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);
kryo.setReferences(true);
kryo.register(DateTime.class, new JodaDateTimeSerializer());
kryo.register(java.util.UUID.class, new UUIDSerializer());
kryo.register(net.fortuna.ical4j.model.component.VEvent.class, new Ical4jVEventSerializer());
kryo.register(net.fortuna.ical4j.model.property.CalScale.class, new JavaSerializer());
EstateManager earth = (EstateManager) o;
Output output = new Output(new FileOutputStream(filekyro));
kryo.writeClassAndObject(output, earth.getApplicants().get(0));
output.close();
When a try to serialize i got the following error:
com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg
constructor): net.fortuna.ical4j.model.property.CalScale$ImmutableCalScale
Serialization trace:
properties (net.fortuna.ical4j.model.Calendar)
calendar (core.Calendar)
calendar (core.EstateManager)
earth (core.Country)
country (core.City)
city (core.Applicant)
at com.esotericsoftware.kryo.util.DefaultInstantiatorStrategy.newInstantiatorOf(DefaultInstantiatorStrategy.java:111)
at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1190)
at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1199)
at com.esotericsoftware.kryo.serializers.FieldSerializer.create(FieldSerializer.java:163)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:122)
From my understanding kryo comes with a lot of standard serializers and tries to use one of them, often this field serializer, and I can either write my own serializer for ical4j or with all the drawbacks use the java serializer (which I did in the code above) and even that fails.
I also tried to find a dedicated ical4j-serializer, found this https://github.com/ical4j/ical4j-serializer but in my opinion I can't use this with kryo, because it does not implement kryo interface, right?
Or is there a problem, that I provide a serializer for "net.fortuna.ical4j.model.property.CalScale" but not for "net.fortuna.ical4j.model.property.CalScale$ImmutableCalScale"? If so, how would I do that?
kryo.register(net.fortuna.ical4j.model.property.CalScale$ImmutableCalScale, new JavaSerializer());
fails also (cannot be resolved to a type)
So what are my options here or what am I missing? Any help well appreciated :-)
If you are building your calendar object in code (i.e. as opposed to parsing a file), you may want to try without immutable properties.
For example, with CalScale you could try:
Whilst immutable properties don't have a default constructor, the mutable properties do.