magro / kryo-serializers

More kryo serializers
Apache License 2.0
381 stars 120 forks source link

Guava 24.1+ issue. Missing serializers for new JdkBackedImmutable* classes #92

Open matt-preston opened 6 years ago

matt-preston commented 6 years ago

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)
matt-preston commented 6 years ago

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());
}
magro commented 5 years ago

Thanks for reporting! Do you want to submit a fix for this?