我想像下面的类一样对方法输出进行序列化。
@Root
public class Camera {
public enum Mode {
CONTINIOUS, TRIGGERED, DEACTIVATED;
}
@Element
private Mode currMode;
@Element
private int interval;
@Element
private int beforeEvent;
@Element
private int afterEvent;
@Element
private String savePath;
public Camera(Mode currMode, int interval, int beforeEvent, int afterEvent,
String savePath) {
super();
this.currMode = currMode;
this.interval = interval;
this.beforeEvent = beforeEvent;
this.afterEvent = afterEvent;
this.savePath = savePath;
}
@ElementList(entry = "mode", name = "availableModes")
public ArrayList<Mode> getAvailableModes() {
return new ArrayList<Mode>(Arrays.asList(Mode.values()));
}
}
该方法将枚举的所有可能性返回为 List。如果没有设置默认构造函数,则序列化成功完成。
<camera>
<savePath>/images</savePath>
<currMode>CONTINIOUS</currMode>
<beforeEvent>5000</beforeEvent>
<interval>3000</interval>
<afterEvent>10000</afterEvent>
<availableModes>
<mode>CONTINIOUS</mode>
<mode>TRIGGERED</mode>
<mode>DEACTIVATED</mode>
</availableModes>
</camera>
但是,反序列化需要默认构造函数。如果我设置了默认构造函数,在序列化和反序列化过程中会出现以下错误。
Default constructor can not accept read only @org.simpleframework.xml.ElementList(data=false, empty=true, entry=mode, inline=false, name=availableModes, required=false, type=void) on method 'availableModes' in class Camera
在反序列化期间,应忽略带注释的方法。有没有可能定义一个元素,在序列化过程中会考虑,而在反序列化过程中会忽略?