【问题标题】:How to retrieve object from key value database into Map, then convert back to object in Java如何从键值数据库中检索对象到 Map,然后转换回 Java 中的对象
【发布时间】:2021-12-30 20:17:54
【问题描述】:

我将一个对象作为值存储在 LevelDB 中。对于 LevelDB,key 和 value 都必须以字节为单位。

我通过套接字接收一个对象并将其转换为 MyObject:

MyObject myObject = (MyObject) (objectInput.readObject());

然后我在 LevelDB 中存储值时序列化我的对象:

myLevelDb().put(bytes((publicKey)), Serializer.serialize(myObject));

序列化器代码如下:

public static byte[] serialize(Object object) {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(bos);
            out.writeObject(object);
            out.flush();
            byte[] yourBytes = bos.toByteArray();
            return yourBytes;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
            } catch (IOException ex) {
                // ignore close exception
            }
        } return null;
    }

然后我尝试遍历 LevelDB 并将每个对象存储到 Map 中。这是我试图将字节反序列化回 MyObject 并将它们保存到 Map 的地方:

private void iterateBytes() {
    DBIterator iterator = myLevelDb().iterator();
    while (iterator.hasNext()) {
        Map.Entry<byte[], byte[]> next = iterator.next();

        String keyString = new String(next.getKey());
        MyObject myObject = (MyObject) Serializer.deserialize(next.getValue());


        Map<String, MyObject> myMap = new HashMap<>();
        myMap().put(keyString, myObject);
    }
}

但是,Java 不允许我在使用以下代码反序列化后将反序列化的字节转换回 MyObject:

public static Object deserialize(byte[] bytes) {
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInput in = null;
        try {
            in = new ObjectInputStream(bis);
            Object o = in.readObject();

            return o;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                // ignore close exception
            }
        } return null;
    }

当我遵循完全相同的序列化和反序列化方法时,我不明白为什么我无法将对象从 byte[] 转换回来。 MyObject 实现了 Serializable 并且 SUID 是正确的,因为它适用于设备之间的 API 调用。我只是不能将它作为原始对象添加到 Map 中,Java 也不会让我转换它。

这是抛出错误的行,无论我在哪里尝试将其转换回 myObject:

    MyObject myObject = (MyObject) Serializer.deserialize(next.getValue());

错误:

class java.lang.String cannot be cast to class myPackage.MyObject (java.lang.String is in module java.base of loader 'bootstrap';

【问题讨论】:

    标签: java arrays serialization casting hashmap


    【解决方案1】:

    通过在序列化时将通用 Object 对象转换为 MyObject(用户定义的类对象)并在反序列化时将其转换来解决此问题。

    这是序列化时的代码:

       public static byte[] serialize(MyObject myObject) {
    
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream out = null;
            try {
                out = new ObjectOutputStream(bos);
                out.writeObject(myObject);
                out.flush();
                byte[] yourBytes = bos.toByteArray();
                return yourBytes;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    bos.close();
                } catch (IOException ex) {
                    // ignore close exception
                }
            } return null;
        }
    

    这是反序列化的代码。

     public static MyObject deserializeNodeClient(byte[] bytes) {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInput in = null;
            try {
                in = new ObjectInputStream(bis);
                MyObject myObject = (MyObject) in.readObject();
    
                return myObject;
    
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException ex) {
                    // ignore close exception
                }
            } return null;
        }
    

    因此,尽管原始 OP 代码在某些情况下工作正常,但无法通过使用通用 Object 对象序列化然后强制转换输出来派生原始对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2021-11-09
      • 2022-01-06
      • 2021-01-26
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多