kryo icon indicating copy to clipboard operation
kryo copied to clipboard

EnumMap not supported

Open fabienrenaud opened this issue 9 years ago • 8 comments

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
    }
}

fabienrenaud avatar Jan 07 '17 18:01 fabienrenaud

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?

magro avatar Jan 08 '17 10:01 magro

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?

fabienrenaud avatar Jan 08 '17 17:01 fabienrenaud

It's here: https://github.com/magro/kryo-serializers/blob/master/src/main/java/de/javakaffee/kryoserializers/EnumMapSerializer.java

magro avatar Jan 08 '17 19:01 magro

Whats the status of this change?

Avery-Whitaker avatar Feb 21 '18 09:02 Avery-Whitaker

Vote up last question....any status update in this?

whipit avatar Feb 27 '18 11:02 whipit

A pull request could still be submitted ;-)

magro avatar Mar 27 '18 09:03 magro

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.

NathanSweet avatar Jun 09 '18 00:06 NathanSweet

You are a hero @NathanSweet!

Avery-Whitaker avatar Jun 14 '18 22:06 Avery-Whitaker