EnumMap not supported
com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): java.util.EnumMap
Exception:
kryotest.CopyTest > main FAILED
com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): java.util.EnumMap
Serialization trace:
map (kryotest.CopyTest$Foo)
at com.esotericsoftware.kryo.Kryo$DefaultInstantiatorStrategy.newInstantiatorOf(Kryo.java:1310)
at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1127)
at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1136)
at com.esotericsoftware.kryo.serializers.MapSerializer.createCopy(MapSerializer.java:168)
at com.esotericsoftware.kryo.serializers.MapSerializer.copy(MapSerializer.java:172)
at com.esotericsoftware.kryo.serializers.MapSerializer.copy(MapSerializer.java:39)
at com.esotericsoftware.kryo.Kryo.copy(Kryo.java:914)
at com.esotericsoftware.kryo.serializers.UnsafeCacheFields$UnsafeObjectField.copy(UnsafeCacheFields.java:315)
at com.esotericsoftware.kryo.serializers.FieldSerializer.copy(FieldSerializer.java:682)
at com.esotericsoftware.kryo.Kryo.copy(Kryo.java:914)
at kryotest.CopyTest.main(CopyTest.java:14)
Full test below.
File: src/test/java/kryotest/CopyTest.java
package kryotest;
import org.junit.*;
import com.esotericsoftware.kryo.Kryo;
import java.util.*;
public class CopyTest {
@Test
public void main() {
Foo f = new Foo();
Kryo k = new Kryo();
Foo copy = k.copy(f);
System.out.println(copy);
}
static class Foo {
Map<MyEnum, String> map = new EnumMap<>(MyEnum.class);
}
enum MyEnum { A, B }
}
build.gradle:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.esotericsoftware', name: 'kryo', version: '4.0.0'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
testLogging {
exceptionFormat = 'full'
showStandardStreams = true
}
}
You could use the EnumMapSerializer from kryo-serializers. However I'd say we should add EnumMapSerializer to kryo itself. Do you want to submit a pull request?
I can't find EnumMapSerializer in the kryo repo: https://github.com/EsotericSoftware/kryo/search?utf8=%E2%9C%93&q=EnumMapSerializer nor any 'kryo-serializers' project in the EsotericSoftware org.
Am I missing something here?
It's here: https://github.com/magro/kryo-serializers/blob/master/src/main/java/de/javakaffee/kryoserializers/EnumMapSerializer.java
Whats the status of this change?
Vote up last question....any status update in this?
A pull request could still be submitted ;-)
I suggest using the EnumMapSerializer from kryo-serializers.
Note that EnumMapSerializer is unsafe (it accesses private API). We could write a safe serializer, but it needs to know the enum class which EnumMap doesn't provide.
You are a hero @NathanSweet!