fury icon indicating copy to clipboard operation
fury copied to clipboard

[Java] deserialize object,error 'A cannot be cast to class B' occurred

Open yyjyc opened this issue 11 months ago • 3 comments

Question

I use fury in my projects, in one of the projects i serialize the object of ProductPO.class where fury is created like "furySerializer" in following code, in the other project i deseralize the byte[] using fury like “furyDeSerializer1” in following code. however,when i deseralize the byte[],an error occurred and made me confused:

java.lang.ClassCastException: class ...RateDetailPO cannot be cast to class ...ProductPO (...RateDetailPO and ...ProductPO are in unnamed module of loader 'app')
	at ...FuryTestV2.test1(FuryTestV2.java:73)
	at ...main(FuryTestV2.java:63)

my code is like:

public class FuryTestV2{
    private static ThreadSafeFury furySerializer = null;
    private static ThreadSafeFury furyDeSerializer1 = null;
    private static ThreadSafeFury furyDeSerializer2 = null;
    static {
        furySerializer = new ThreadLocalFury(classLoader -> {
            Fury f = Fury.builder().withCompatibleMode(CompatibleMode.COMPATIBLE).withLanguage(Language.JAVA).withClassLoader(classLoader)
                    .withDeserializeNonexistentClass(true).build();
            f.register(ProductPO.class);
            return f;
        });

        furyDeSerializer1 = new ThreadLocalFury(classLoader -> {
            Fury f = Fury.builder().withCompatibleMode(CompatibleMode.COMPATIBLE).withLanguage(Language.JAVA).withClassLoader(classLoader)
                    .withDeserializeNonexistentClass(true).build();
            f.register(RateDetailPO.class);
            f.register(PersonRulePO.class);
            f.register(DailyInfoPO.class);
            f.register(DailyInfoList.class);
            f.register(GregorianCalendar.class);
            f.register(com.ctrip.hotel.productsearchfilter.ProductPO.class);
            return f;
        });

        furyDeSerializer2 = new ThreadLocalFury(classLoader -> {
            Fury f = Fury.builder().withCompatibleMode(CompatibleMode.COMPATIBLE).withLanguage(Language.JAVA).withClassLoader(classLoader)
                    .withDeserializeNonexistentClass(true).build();
            f.register(com.ctrip.hotel.productsearchfilter.ProductPO.class);
            f.register(RateDetailPO.class);
            f.register(PersonRulePO.class);
            f.register(DailyInfoPO.class);
            f.register(DailyInfoList.class);
            f.register(GregorianCalendar.class);
            return f;
        });
    }

    public static void main(String[] args) {
        // 构建序列化的对象
        ProductPO productPO = createProductPO();
        // 序列化
        System.out.println("test1:" + test1(productPO)); // false
        System.out.println("test2:" + test2(productPO)); // true

    }

    private static boolean test1(ProductPO productPO) {
        try {
            // 序列化
            byte[] serialize = furySerializer.serialize(productPO);
            // 反序列化
            com.ctrip.hotel.productsearchfilter.ProductPO result = (com.ctrip.hotel.productsearchfilter.ProductPO) furyDeSerializer1.deserialize(serialize);
            System.out.println(SSJsonSerializerUtils.serialize(result));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    private static boolean test2(ProductPO productPO) {
        try {
            // 序列化
            byte[] serialize = furySerializer.serialize(productPO);
            // 反序列化
            com.ctrip.hotel.productsearchfilter.ProductPO result = (com.ctrip.hotel.productsearchfilter.ProductPO) furyDeSerializer2.deserialize(serialize);
            System.out.println(SSJsonSerializerUtils.serialize(result));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static ProductPO createProductPO() {
        ProductPO productPO = new ProductPO();
        productPO.setProductId(1112324L);
        productPO.setHotelId(1234);
        productPO.setMasterHotelId(1235);
        productPO.setBasicRoomId(54435);
        productPO.setMasterBasicRoomId(6234);
        productPO.setRateCodeId(564332);
        productPO.setVendorId(6564);
        productPO.setRoomClass(6563423525L);
        productPO.setPerson(5);
        productPO.setResourceType(3);
        productPO.setPromoteRoomType(true);
        return productPO;
    }

}

yyjyc avatar Jan 13 '25 10:01 yyjyc