简体   繁体   English

kryo 序列化程序序列化 iCal4j 日历失败“无法创建类(缺少无参数构造函数)”

[英]kryo serializer serializing iCal4j Calendar fails "Class cannot be created (missing no-arg constructor)"

I try to implement kryo into an existing project.我尝试将 kryo 实施到现有项目中。

My project has a class "Calender", with an iCal4j object:我的项目有一个 class“日历”,带有 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 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.据我了解,kryo 带有许多标准序列化程序并尝试使用其中一个,通常是这个字段序列化程序,我可以为 ical4j 编写自己的序列化程序,或者使用 java 序列化程序(我在代码中做了)以上),甚至失败。

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?我还试图找到一个专用的 ical4j-serializer,找到了这个https://github.com/ical4j/ical4j-serializer但我认为我不能将它与 kryo 一起使用,因为它没有实现 kryo 接口,对吧?

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"?还是有问题,我为“net.fortuna.ical4j.model.property.CalScale”提供了一个序列化程序,但没有为“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 (ie as opposed to parsing a file), you may want to try without immutable properties.如果您在代码中构建日历 object(即与解析文件相反),您可能想尝试不使用不可变属性。

For example, with CalScale you could try:例如,使用 CalScale,您可以尝试:

new CalScale(CalScale.VALUE_GREGORIAN)

Whilst immutable properties don't have a default constructor, the mutable properties do.虽然不可变属性没有默认构造函数,但可变属性有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM