I would like to add serialization support with XMLEncoder/XMLDecoder to a case class in Scala. Here’s my attempt:
@ConstructorProperties(Array("sequenceNumber", "nanosecondsTotal", "bytesTotal", "timeMillis"))
final case class IoStatistics(
@BeanProperty sequenceNumber: Long,
@BeanProperty nanosecondsTotal: Long,
@BeanProperty bytesTotal: Long,
@BeanProperty timeMillis: Long = System.currentTimeMillis
) {
...
}
I can verify with javap that the generated class indeed has the public four-argument constructor and the appropriate bean getters for the properties. However, when trying to serialize an instance with XMLEncoder I get the following console output:
java.lang.InstantiationException: net.java.truevfs.ext.jmx.model.IoStatistics
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(IoStatistics);
Continuing ...
and the output is:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_06" class="java.beans.XMLDecoder">
</java>
It seems the annotation is not applied to the constructor. How can I verify and fix this?
I don’t know anything about XML serialization, but maybe you should apply the
ConstructorPropertiesannotation to the constructor instead of to the class?