简体   繁体   English

我可以将Java序列化对象反序列化为不同的类吗?

[英]Can I DeSerialize a java serialized object to different classes?

Unsure how to frame the question. 不确定如何提出问题。 Here is a try : 尝试一下:

I have two aerospike caches (sets) say X, Y. 我有两个气雾缓存(集合),分别是X,Y。

there are two versions of a jar each has a class com.xyz.MyClass 一个罐子有两个版本,每个版本都有一个com.xyz.MyClass类

jar1 : jar1:

com.xyz.MyClass{
    com.abc.Request request;
    ...
}

jar2 : jar2:

com.xyz.MyClass{
    com.pqr.Request request;
    ...
}

Now Some objects of MyClass from jar1 are stored in cache X and some objects of MyClass from jar2 are stored in cache Y. 现在,jar1中的MyClass的某些对象存储在缓存X中,而jar2中的MyClass的某些对象存储在缓存Y中。

I want to deserialize data from both the caches, perform some computation and store in a db. 我想反序列化两个缓存中的数据,执行一些计算并存储在数据库中。

I can't serialize as the fully qualified name of MyClass in both jars is same. 我无法序列化,因为两个罐子中MyClass的完全限定名称是相同的。

I will remove one of the caches (but that will take some time). 我将删除其中一个缓存(但这将需要一些时间)。 So, Hoping for a temporary fix to handle this situation for now. 因此,希望暂时解决此问题。

Can i handle this in a nice way? 我可以很好地处理吗?

Maybe deserialize into something intermediary(that doesn't care about packages, but only class structure) and then to MyClass. 也许反序列化为某种中介(不关心包,而仅关心类结构),然后反序列化为MyClass。

Note : both the classes have same structure, just package name of a member variable is different. 注意:这两个类具有相同的结构,只是成员变量的包名称不同。

PS : Don't want to use multiple class loaders. PS:不想使用多个类加载器。

It will take huge amount of magic to achive it, and disallowing classloader magic remove half of your options. 要实现它,将需要大量的魔术,而禁止classloader魔术会删除您一半的选择。

Solution that I can propose is editing serialized data. 我可以提出的解决方案是编辑序列化数据。 Format in which object are serialized is known and is part of Java spec. 对象序列化的格式是已知的,并且是Java规范的一部分。 So you can edit this bytes to replace one class name to another, but I suggest you think twice before you start to implement this. 因此,您可以编辑此字节以将一个类名替换为另一个类名,但是我建议您在开始实现之前三思而后行。 It may be not so trivial task. 这可能不是那么琐碎的任务。

Generate two different serialVersionUID's and use them in each of the class. 生成两个不同的serialVersionUID ,并在每个类中使用它们。

private static final long serialVersionUID = -7000188057019447678L;// Add this in MyClass of jar1

private static final long serialVersionUID = -3944128180162145709L;// Add this in MyClass of jar2

You can generate your own serialVersionUID's using Eclipse IDE or JDK's serialver tool . 您可以使用Eclipse IDEJDK的serialver工具生成自己的serialVersionUID。

Note : If no serialVersionUID is declared, JVM will use its own algorithm to generate a default SerialVersionUID. 注意 :如果未声明serialVersionUID,则JVM将使用其自己的算法来生成默认的SerialVersionUID。 you can check the algorithm here. 您可以在此处检查算法

The default serialVersionUID computation is highly sensitive to class details and may vary from different JVM implementation, and result in an unexpected InvalidClassExceptions during the deserialization process. 默认的serialVersionUID计算对类详细信息高度敏感,并且可能因不同的JVM实现而异,并且在反序列化过程中会导致意外的InvalidClassExceptions。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM