使用不同的包名称对不同应用程序中的java对象进行de / serialize [英] de/serialize java objects across different applications using different package names

查看:109
本文介绍了使用不同的包名称对不同应用程序中的java对象进行de / serialize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不同的应用程序之间共享java对象。

I want to share java objects across different applications.

只要我在不同的项目中使用相同的包名称,它就可以正常工作。
但是如果我更改包名称它就不再起作用了。

As long as I use the same package names in the different projects it works fine. But if I change the package names it doesn't work anymore.

我试图通过扩展 ObjectInputStream来解决这个问题。 code> class并覆盖 readClassDescriptor 方法。

但是这样做会出现以下错误:

But by doing so I get the following error:

java.io.StreamCorruptedException: invalid type code: 00

...不知道如何解决这个问题。

... dont know how to solve this problem.

这是我用于扩展ObjectInputStream的代码class:

Here is the code I use for the extended ObjectInputStream class:

public class MyObjectInputStream extends ObjectInputStream {

    public static Map<String, Class> classNameMapping = initclassNameMapping(); 

    private static Map<String, Class> initclassNameMapping(){
        Map<String, Class> res = new HashMap<String, Class>();
        //ipxTest is the name of the package where the objects got serialized
        res.put("ipxTest.IPX", interprojectxchangeTest.IPX.class); 
        res.put("ipxTest.A", interprojectxchangeTest.A.class);
        return Collections.unmodifiableMap(res);
    }

    public MyObjectInputStream(InputStream in) throws IOException {
        super(in);
        enableResolveObject(true);
    }


    protected MyObjectInputStream() throws IOException, SecurityException {
        super();
        enableResolveObject(true);
    }

    @Override
    protected java.io.ObjectStreamClass readClassDescriptor() 
            throws IOException, ClassNotFoundException {
        ObjectStreamClass desc = super.readClassDescriptor();
        if (classNameMapping.containsKey(desc.getName()))
            return ObjectStreamClass.lookup(classNameMapping.get(desc.getName()));
        return desc;
    }
}

IPX和A类在不同方面看起来相同项目并且每个都有相同的serialID。

The IPX and A classes both look equal in the different projects and have each the same serialID.

推荐答案

我的第一个建议是让你的实现变得简单并停止对抗框架 - 使用跨应用程序的相同包名称。我建议从可序列化的类中创建一个库,并在实现中共享它。

My first suggestion is to make your implementation simple and stop fighting the framework - use the same package names across applications. I would suggest making a library out of the serializable classes, and sharing that among the implementations.

如果你必须在具有不同包名的应用程序之间进行序列化/反序列化,那么我的建议将放弃内置的Java序列化,它与类名和包名紧密相关,并使用类似Gson的序列化/反序列化。

If you MUST serialize / deserialize across applications with different package names, then my suggestion would be to forego the built-in Java serialization, which is tightly tied to the class name and package name, and use something like Gson to serialize / deserialize.

Gson允许你要指定一个TypeAdaper。您可以为要序列化/反序列化的每个类创建和注册TypeAdapter,并在序列化时将类名称(没有包名称)指定为类型,如下例所示,但使用getSimpleName()而不是getCanonicalName( )

Gson allows you to specify a TypeAdaper. You can create and register a TypeAdapter for each class that you will serialize/deserialize, and specify the class name (without the package name) as the 'type' when serializing, like the following example, but use getSimpleName() instead of getCanonicalName()

反序列化时,您必须将正确的包名称添加到类型

When deserializing, you'd have to add the correct package name to the "type"

您必须为每个应用程序单独执行TypeAdapter。

You'd have to do the TypeAdapters individually for each application.

public class GsonTypeAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
    @Override
    public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject result = new JsonObject();
        result.add("type", new JsonPrimitive(src.getClass().getCanonicalName()));
        result.add("properties", context.serialize(src, src.getClass()));

        return result;
    }

    @Override
    public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
        JsonObject jsonObject = json.getAsJsonObject();
        String type = jsonObject.get("type").getAsString();
        JsonElement element = jsonObject.get("properties");

        try {
            return context.deserialize(element, Class.forName(type));
        } catch (ClassNotFoundException cnfe) {
            throw new JsonParseException("Unknown element type: " + type, cnfe);
        }
    }
}

这篇关于使用不同的包名称对不同应用程序中的java对象进行de / serialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆