EsotericSoftware / kryo

Java binary serialization and cloning: fast, efficient, automatic
BSD 3-Clause "New" or "Revised" License
6.19k stars 823 forks source link

Can kryo support the functional object of google guava #852

Closed jackjoesh closed 3 years ago

jackjoesh commented 3 years ago

Sometimes, we use the "Lists.transform" of google guava's api to convert the do object to vo object as the following:

    List<WithdrawErrorLogQueryFeVo> rows = Collections.emptyList();
    if(total > 0){
        List<WithdrawErrorLog> list = withdrawErrorLogMapper.selectByCondition(request.getUserId(), null, startCreateTime, offset, size);
        rows = Lists.transform(list, e->{//**convert the do object to vo object** 
            WithdrawErrorLogQueryFeVo vo = new WithdrawErrorLogQueryFeVo();
            BeanUtils.copyProperties(e, vo);
            vo.setAmount(e.getAmount() == null ? null : e.getAmount().toPlainString());
            vo.setTime(e.getCreateTime().getTime());
            return vo;
        });
    }

But "Lists.transform" only return the lazy functional object from provider service, and kryo will not execute functional object, just return. That will return the do object to the customer service(not vo object , because lazy functional object), so the kryo deserialize of customer service will throw exception because it hasn't do object.

Can we support executing functional object of guava or jdk in kryo, like that:

List executeRows = Lists.newCopyOnWriteArrayList((AbstractList)rows); //convert functional rows to common list executeRows

Convert guava list to a common list, and continue serializing. So it will not return the functional object.

theigl commented 3 years ago

@jackjoesh: Kryo only supports built-in JDK classes. Support for some Guava classes can be found in https://github.com/magro/kryo-serializers. But there is no serializer for Guava's Transforming*Lists.

It should be quite easy to write and register a custom serializer for these classes. Take a look at the implementations JdkImmutableListSerializer for instance.

You could override the write method to something like this:

@Override
public void write (Kryo kryo, Output output, T collection) {
    super.write(kryo, output, new ArrayList(collection));
}
jackjoesh commented 3 years ago

JdkImmutableListSerializer

thank you very much , I will try this, it's a good idea