Java Code Examples for de.javakaffee.kryoserializers.UnmodifiableCollectionsSerializer#registerSerializers()

The following examples show how to use de.javakaffee.kryoserializers.UnmodifiableCollectionsSerializer#registerSerializers() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KryoSerialization.java    From JPPF with Apache License 2.0 6 votes vote down vote up
/**
 * Create a Kryo instance, with its instantiator strategy and a set of
 * common serializers (from kryo-serializers project) initialized.
 * @return an instance of {@link Kryo}.
 */
private static Kryo createKryo() {
  final Kryo kryo = new Kryo(new CustomClassResolver(), new MapReferenceResolver());
  kryo.setAutoReset(true);
  kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));

  kryo.register(ObjectName.class, new ObjectNameSerializer());
  kryo.register(OffloadableNotification.class, new GenericObjectSerializer());
  kryo.register(TaskExecutionNotification.class, new GenericObjectSerializer());
  kryo.register(TaskGraph.class, new JobTaskGraphSerializer());
  kryo.register(Collection.class, new CollectionSerializer());
  kryo.register(Arrays.asList( "" ).getClass(), new ArraysAsListSerializer() );
  kryo.register(Collections.EMPTY_LIST.getClass(), new CollectionsEmptyListSerializer() );
  kryo.register(Collections.EMPTY_MAP.getClass(), new CollectionsEmptyMapSerializer() );
  kryo.register(Collections.EMPTY_SET.getClass(), new CollectionsEmptySetSerializer() );
  kryo.register(Collections.singletonList( "" ).getClass(), new CollectionsSingletonListSerializer() );
  kryo.register(Collections.singleton( "" ).getClass(), new CollectionsSingletonSetSerializer() );
  kryo.register(Collections.singletonMap( "", "" ).getClass(), new CollectionsSingletonMapSerializer() );
  kryo.register(GregorianCalendar.class, new GregorianCalendarSerializer());
  kryo.register(InvocationHandler.class, new JdkProxySerializer());
  UnmodifiableCollectionsSerializer.registerSerializers(kryo);
  SynchronizedCollectionsSerializer.registerSerializers(kryo);
  kryo.register(EnumMap.class, new EnumMapSerializer());
  kryo.register(EnumSet.class, new EnumSetSerializer());
  return kryo;
}
 
Example 2
Source File: KryoSerialization.java    From eagle with Apache License 2.0 6 votes vote down vote up
protected Kryo initialValue() {
    Kryo kryo = new Kryo();
    kryo.register(CGLibProxySerializer.CGLibProxyMarker.class, new CGLibProxySerializer());
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
    SynchronizedCollectionsSerializer.registerSerializers(kryo);
    kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
    ImmutableListSerializer.registerSerializers(kryo);
    ImmutableSetSerializer.registerSerializers(kryo);
    ImmutableMapSerializer.registerSerializers(kryo);
    ImmutableMultimapSerializer.registerSerializers(kryo);
    ReverseListSerializer.registerSerializers(kryo);
    UnmodifiableNavigableSetSerializer.registerSerializers(kryo);
    ArrayListMultimapSerializer.registerSerializers(kryo);
    HashMultimapSerializer.registerSerializers(kryo);
    LinkedHashMultimapSerializer.registerSerializers(kryo);
    LinkedListMultimapSerializer.registerSerializers(kryo);
    TreeMultimapSerializer.registerSerializers(kryo);
    return kryo;
}
 
Example 3
Source File: TreeSerializationTest.java    From swblocks-decisiontree with Apache License 2.0 5 votes vote down vote up
@Test
public void treeNode() {
    final Result<DecisionTreeRuleSet> result = (new CommisionRuleSetSupplier()).get();
    EhSupport.ensure(result.isSuccess(), "Could not create decision tree");
    final DecisionTreeRuleSet ruleSet = result.getData();

    final TreeNode node = DecisionTreeFactory.constructDecisionTree(ruleSet, DecisionTreeType.SINGLE);

    final Kryo kryo = new Kryo();
    // no default no-arg constructors
    kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());

    final InstantiatorStrategy defaultInstantiatorStrategy = new Kryo.DefaultInstantiatorStrategy();
    kryo.getRegistration(ArrayList.class)
            .setInstantiator(defaultInstantiatorStrategy.newInstantiatorOf(ArrayList.class));
    kryo.getRegistration(HashSet.class)
            .setInstantiator(defaultInstantiatorStrategy.newInstantiatorOf(HashSet.class));
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Output output = new Output(out);
    kryo.writeObject(output, node);
    output.flush();
    output.close();

    final ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
    final Input kryoInput = new Input(inputStream);
    final TreeNode tree = kryo.readObject(kryoInput, BaseTreeNode.class);

    final SingleDecisionTreeFactoryTest test = new SingleDecisionTreeFactoryTest();
    test.checkTreeNode(tree, ruleSet);

    assertEquals(node, tree);
}
 
Example 4
Source File: KryoSerializer.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of Kryo serializer, for use with the given object type.
 * <p/>
 * Note: this method is public to allow end-users to validate compatibility of their POJOs,
 * with the Kryo serializer as used by CQEngine.
 *
 * @param objectType The type of object which the instance of Kryo will serialize
 * @return a new instance of Kryo serializer
 */
@SuppressWarnings({"ArraysAsListWithZeroOrOneArgument", "WeakerAccess"})
protected Kryo createKryo(Class<?> objectType) {
    Kryo kryo = new Kryo();
    // Instantiate serialized objects via a no-arg constructor when possible, falling back to Objenesis...
    kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    // Register the object which this index will persist...
    kryo.register(objectType);
    kryo.setRegistrationRequired(false);
    // Register additional serializers which are not built-in to Kryo 3.0...
    kryo.register(Arrays.asList().getClass(), new ArraysAsListSerializer());
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
    SynchronizedCollectionsSerializer.registerSerializers(kryo);
    return kryo;
}
 
Example 5
Source File: DbStoragePlainFile.java    From Paper with Apache License 2.0 5 votes vote down vote up
private Kryo createKryoInstance(boolean compatibilityMode) {
    Kryo kryo = new Kryo();

    if (compatibilityMode) {
        kryo.getFieldSerializerConfig().setOptimizedGenerics(true);
    }

    kryo.register(PaperTable.class);
    kryo.setDefaultSerializer(CompatibleFieldSerializer.class);
    kryo.setReferences(false);

    // Serialize Arrays$ArrayList
    //noinspection ArraysAsListWithZeroOrOneArgument
    kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
    SynchronizedCollectionsSerializer.registerSerializers(kryo);
    // Serialize inner AbstractList$SubAbstractListRandomAccess
    kryo.addDefaultSerializer(new ArrayList<>().subList(0, 0).getClass(),
            new NoArgCollectionSerializer());
    // Serialize AbstractList$SubAbstractList
    kryo.addDefaultSerializer(new LinkedList<>().subList(0, 0).getClass(),
            new NoArgCollectionSerializer());
    // To keep backward compatibility don't change the order of serializers above

    // UUID support
    kryo.register(UUID.class, new UUIDSerializer());

    for (Class<?> clazz : mCustomSerializers.keySet())
        kryo.register(clazz, mCustomSerializers.get(clazz));

    kryo.setInstantiatorStrategy(
            new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));

    return kryo;
}
 
Example 6
Source File: SerDeUtils.java    From metron with Apache License 2.0 5 votes vote down vote up
@Override
    protected Kryo initialValue() {
      Kryo ret = new Kryo();
      ret.setReferences(true);
      ret.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));

      ret.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
      ret.register(Collections.EMPTY_LIST.getClass(), new CollectionsEmptyListSerializer());
      ret.register(Collections.EMPTY_MAP.getClass(), new CollectionsEmptyMapSerializer());
      ret.register(Collections.EMPTY_SET.getClass(), new CollectionsEmptySetSerializer());
      ret.register(Collections.singletonList("").getClass(), new CollectionsSingletonListSerializer());
      ret.register(Collections.singleton("").getClass(), new CollectionsSingletonSetSerializer());
      ret.register(Collections.singletonMap("", "").getClass(), new CollectionsSingletonMapSerializer());
      ret.register(GregorianCalendar.class, new GregorianCalendarSerializer());
      ret.register(InvocationHandler.class, new JdkProxySerializer());
      UnmodifiableCollectionsSerializer.registerSerializers(ret);
      SynchronizedCollectionsSerializer.registerSerializers(ret);

// custom serializers for non-jdk libs

// register CGLibProxySerializer, works in combination with the appropriate action in handleUnregisteredClass (see below)
      ret.register(CGLibProxySerializer.CGLibProxyMarker.class, new CGLibProxySerializer());
// joda DateTime, LocalDate and LocalDateTime
      ret.register(LocalDate.class, new JodaLocalDateSerializer());
      ret.register(LocalDateTime.class, new JodaLocalDateTimeSerializer());
// guava ImmutableList, ImmutableSet, ImmutableMap, ImmutableMultimap, UnmodifiableNavigableSet
      ImmutableListSerializer.registerSerializers(ret);
      ImmutableSetSerializer.registerSerializers(ret);
      ImmutableMapSerializer.registerSerializers(ret);
      ImmutableMultimapSerializer.registerSerializers(ret);
      return ret;
    }
 
Example 7
Source File: SerDeUtils.java    From metron with Apache License 2.0 5 votes vote down vote up
@Override
protected Kryo initialValue() {
  Kryo ret = new Kryo();
  ret.setReferences(true);
  ret.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));

  ret.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
  ret.register(Collections.EMPTY_LIST.getClass(), new CollectionsEmptyListSerializer());
  ret.register(Collections.EMPTY_MAP.getClass(), new CollectionsEmptyMapSerializer());
  ret.register(Collections.EMPTY_SET.getClass(), new CollectionsEmptySetSerializer());
  ret.register(Collections.singletonList("").getClass(), new CollectionsSingletonListSerializer());
  ret.register(Collections.singleton("").getClass(), new CollectionsSingletonSetSerializer());
  ret.register(Collections.singletonMap("", "").getClass(), new CollectionsSingletonMapSerializer());
  ret.register(GregorianCalendar.class, new GregorianCalendarSerializer());
  ret.register(InvocationHandler.class, new JdkProxySerializer());
  UnmodifiableCollectionsSerializer.registerSerializers(ret);
  SynchronizedCollectionsSerializer.registerSerializers(ret);

  // custom serializers for non-jdk libs

  // register CGLibProxySerializer, works in combination with the appropriate action in handleUnregisteredClass (see below)
  ret.register(CGLibProxySerializer.CGLibProxyMarker.class, new CGLibProxySerializer());

  // joda DateTime, LocalDate and LocalDateTime
  ret.register(LocalDate.class, new JodaLocalDateSerializer());
  ret.register(LocalDateTime.class, new JodaLocalDateTimeSerializer());

  // guava ImmutableList, ImmutableSet, ImmutableMap, ImmutableMultimap, UnmodifiableNavigableSet
  ImmutableListSerializer.registerSerializers(ret);
  ImmutableSetSerializer.registerSerializers(ret);
  ImmutableMapSerializer.registerSerializers(ret);
  ImmutableMultimapSerializer.registerSerializers(ret);
  return ret;
}
 
Example 8
Source File: DeepKryoRegistrator.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void registerClasses(Kryo kryo) {
    kryo.register(Cell.class);
    kryo.register(Cells.class);
    kryo.register(IDeepType.class);
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
}
 
Example 9
Source File: KryoUtils.java    From reef with Apache License 2.0 5 votes vote down vote up
@Inject
private KryoUtils() {
  final KryoFactory factory = new KryoFactory() {
    @Override
    public Kryo create() {
      final Kryo kryo = new Kryo();
      UnmodifiableCollectionsSerializer.registerSerializers(kryo); // Required to serialize/deserialize Throwable
      return kryo;
    }
  };
  kryoPool = new KryoPool.Builder(factory).softReferences().build();
}
 
Example 10
Source File: Nd4jRegistrator.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void registerClasses(Kryo kryo) {
    kryo.register(Nd4j.getBackend().getNDArrayClass(), new Nd4jSerializer());
    kryo.register(Nd4j.getBackend().getComplexNDArrayClass(), new Nd4jSerializer());
    kryo.register(AtomicDouble.class, new AtomicDoubleSerializer());

    //Also register Java types (synchronized/unmodifiable collections), which will fail by default
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
    SynchronizedCollectionsSerializer.registerSerializers(kryo);
}
 
Example 11
Source File: Nd4jRegistrator.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void registerClasses(Kryo kryo) {
    kryo.register(Nd4j.getBackend().getNDArrayClass(), new Nd4jSerializer());
    kryo.register(AtomicDouble.class, new AtomicDoubleSerializer());

    //Also register Java types (synchronized/unmodifiable collections), which will fail by default
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
    SynchronizedCollectionsSerializer.registerSerializers(kryo);
}
 
Example 12
Source File: KryoTranscoder.java    From springboot-shiro-cas-mybatis with MIT License 4 votes vote down vote up
/**
 * Initialize and register classes with kryo.
 */
public void initialize() {
    // Register types we know about and do not require external configuration
    kryo.register(ArrayList.class);
    kryo.register(BasicCredentialMetaData.class);
    kryo.register(Class.class, new DefaultSerializers.ClassSerializer());
    kryo.register(Date.class, new DefaultSerializers.DateSerializer());
    kryo.register(HardTimeoutExpirationPolicy.class);
    kryo.register(HashMap.class);
    kryo.register(DefaultHandlerResult.class);
    kryo.register(ImmutableAuthentication.class);
    kryo.register(MultiTimeUseOrTimeoutExpirationPolicy.class);
    kryo.register(NeverExpiresExpirationPolicy.class);
    kryo.register(RememberMeDelegatingExpirationPolicy.class);
    kryo.register(ServiceTicketImpl.class);
    kryo.register(SimpleWebApplicationServiceImpl.class, new SimpleWebApplicationServiceSerializer());
    kryo.register(ThrottledUseAndTimeoutExpirationPolicy.class);
    kryo.register(TicketGrantingTicketExpirationPolicy.class);
    kryo.register(TicketGrantingTicketImpl.class);
    kryo.register(TimeoutExpirationPolicy.class);
    kryo.register(URL.class, new URLSerializer());

    // we add these ones for tests only
    kryo.register(RegisteredServiceImpl.class, new RegisteredServiceSerializer());
    kryo.register(RegexRegisteredService.class, new RegisteredServiceSerializer());

    // new serializers to manage Joda dates and immutable collections
    kryo.register(DateTime.class, new JodaDateTimeSerializer());
    kryo.register(CasDelegatingLogger.class, new DefaultSerializers.VoidSerializer());

    // from the kryo-serializers library (https://github.com/magro/kryo-serializers)
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);

    // Register other types
    if (serializerMap != null) {
        for (final Map.Entry<Class<?>, Serializer> clazz : serializerMap.entrySet()) {
            kryo.register(clazz.getKey(), clazz.getValue());
        }
    }

    // don't reinit the registered classes after every write or read
    kryo.setAutoReset(false);
    // don't replace objects by references
    kryo.setReferences(false);
    // Catchall for any classes not explicitly registered
    kryo.setRegistrationRequired(false);
}
 
Example 13
Source File: KryoTranscoder.java    From cas4.0.x-server-wechat with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize and register classes with kryo.
 */
public void initialize() {
    // Register types we know about and do not require external configuration
    kryo.register(ArrayList.class);
    kryo.register(BasicCredentialMetaData.class);
    kryo.register(Class.class, new DefaultSerializers.ClassSerializer());
    kryo.register(Date.class, new DefaultSerializers.DateSerializer());
    kryo.register(HardTimeoutExpirationPolicy.class);
    kryo.register(HashMap.class);
    kryo.register(HandlerResult.class);
    kryo.register(ImmutableAuthentication.class);
    kryo.register(MultiTimeUseOrTimeoutExpirationPolicy.class);
    kryo.register(NeverExpiresExpirationPolicy.class);
    kryo.register(RememberMeDelegatingExpirationPolicy.class);
    kryo.register(ServiceTicketImpl.class);
    kryo.register(SimpleWebApplicationServiceImpl.class, new SimpleWebApplicationServiceSerializer());
    kryo.register(ThrottledUseAndTimeoutExpirationPolicy.class);
    kryo.register(TicketGrantingTicketExpirationPolicy.class);
    kryo.register(TicketGrantingTicketImpl.class);
    kryo.register(TimeoutExpirationPolicy.class);
    kryo.register(URL.class, new URLSerializer());

    // we add these ones for tests only
    kryo.register(RegisteredServiceImpl.class, new RegisteredServiceSerializer());
    kryo.register(RegexRegisteredService.class, new RegisteredServiceSerializer());

    // new serializers to manage Joda dates and immutable collections
    kryo.register(DateTime.class, new JodaDateTimeSerializer());
    // from the kryo-serializers library (https://github.com/magro/kryo-serializers)
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);

    // Register other types
    if (serializerMap != null) {
        for (final Map.Entry<Class<?>, Serializer> clazz : serializerMap.entrySet()) {
            kryo.register(clazz.getKey(), clazz.getValue());
        }
    }

    // don't reinit the registered classes after every write or read
    kryo.setAutoReset(false);
    // don't replace objects by references
    kryo.setReferences(false);
    // Catchall for any classes not explicitly registered
    kryo.setRegistrationRequired(false);
}
 
Example 14
Source File: UnmodifiableCollectionsRegistrator.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void registerClasses(Kryo kryo) {
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
}
 
Example 15
Source File: KryoPersistence.java    From recheck with GNU Affero General Public License v3.0 3 votes vote down vote up
private static Kryo createKryo() {
	final Kryo kryo = new Kryo();

	kryo.setInstantiatorStrategy( new Kryo.DefaultInstantiatorStrategy( new StdInstantiatorStrategy() ) );

	final Registration registration = kryo.getRegistration( TreeMultiset.class );
	registration.setInstantiator( TreeMultiset::create );

	UnmodifiableCollectionsSerializer.registerSerializers( kryo );

	return kryo;
}