kryo icon indicating copy to clipboard operation
kryo copied to clipboard

fail deserialize HashMap.KeySet

Open GLBB opened this issue 6 years ago • 3 comments

for convience, except use custom serializer, have some method to solve it? Version: 5.0.0-RC4 Exception stack:

com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): java.util.HashMap$KeySet
Serialization trace:
set (cn.gl.g_music_crawler.kryo.User)

	at com.esotericsoftware.kryo.util.DefaultInstantiatorStrategy.newInstantiatorOf(DefaultInstantiatorStrategy.java:98)
	at com.esotericsoftware.kryo.Kryo.newInstantiator(Kryo.java:1105)
	at com.esotericsoftware.kryo.Kryo.newInstance(Kryo.java:1114)
	at com.esotericsoftware.kryo.serializers.CollectionSerializer.create(CollectionSerializer.java:172)
	at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:200)
	at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:43)
	at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:718)
	at com.esotericsoftware.kryo.serializers.ReflectField.read(ReflectField.java:114)
	at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:122)
	at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:696)
	at cn.gl.g_music_crawler.kryo.KryoTest.test3(KryoTest.java:72)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:567)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

example code:

@Data
class User{
    private Map<String, String> info;
    private Set<String> set;
}

        User user = new User();
        HashMap<String, String> map = new HashMap<>();
        map.put("age", "222");
        user.setInfo(map);
        user.setSet(map.keySet());
        Kryo kryo = new Kryo();
        kryo.setRegistrationRequired(false);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Output output = new Output(bos);
        kryo.writeObject(output, user);
        output.close();
        byte[] bytes = bos.toByteArray();

        Input input = new Input(new ByteArrayInputStream(bytes));
        User newUser = kryo.readObject(input, User.class);
        input.close();

GLBB avatar Mar 08 '20 09:03 GLBB

There is no built-in serializer for HashMap.KeySet. You have to write your own or register the standard JavaSerializer for this class.

theigl avatar May 16 '20 16:05 theigl

Thinking about this again: In your example it doesn't really make sense to serialize the KeySet at all. It is just a view of the original map. If you have a case where you know that one field is always a view of another field, I'd make that field transient and lazily create it in the getter.

theigl avatar May 23 '20 08:05 theigl

Closed for now, please reopen if this is still an issue.

magro avatar Jun 01 '20 14:06 magro