kryo-serializers
kryo-serializers copied to clipboard
Guava 24.1+ issue. Missing serializers for new JdkBackedImmutable* classes
Guava 24.1 introduced new implementations of ImmutableSet, ImmutableMultiset, ImmutableMap and ImmutableBiMap. These new implementations do not get registered with kryo by the guava support classes in kryo-serializer.
e.g. ImmutableMapSerializer does not register a serializer for com.google.common.collect.JdkBackedImmutableMap
Serializing Guava ImmutableMap instances with kryo.setRegistrationRequired(true) can lead to Exceptions like this:
java.lang.IllegalArgumentException: Class is not registered: com.google.common.collect.JdkBackedImmutableMap
Note: To register this class use: kryo.register(com.google.common.collect.JdkBackedImmutableMap.class);
at com.esotericsoftware.kryo.Kryo.getRegistration(Kryo.java:503)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:557)
Test case to illustrate the issue
Utility class to instantiate JdkBackedImmutableMap
package com.google.common.collect;
public class JdkBackedImmutableMapBuilder<K, V> extends ImmutableMap.Builder<K, V> {
@Override
public ImmutableMap<K, V> build() {
return super.buildJdkBacked();
}
}
Test case
@Test
public void testJdkBackedImmutableMapSerialization() throws Exception {
final Kryo kryo = new Kryo();
kryo.setRegistrationRequired(true);
kryo.register(HashMap.class);
ImmutableMapSerializer.registerSerializers(kryo);
// Uncomment to pass
// kryo.register(Class.forName("com.google.common.collect.JdkBackedImmutableMap"), new ImmutableMapSerializer());
final Output output = new Output(4096);
kryo.writeObject(output, new JdkBackedImmutableMapBuilder()
.put("a", "b")
.put("c", "d")
.build());
}
Thanks for reporting! Do you want to submit a fix for this?