Java:对象到字节[]和字节[]到对象转换器(用于东京内阁)

发布于 2024-09-19 16:26:24 字数 123 浏览 7 评论 0 原文

我需要将对象转换为 byte[] 以存储在 Tokyo Cabinet 键值存储中。 从键值存储中读取时,我还需要将 byte[] 解字节为对象。

有没有任何软件包可以帮助我完成这项任务?还是自己实施的最佳解决方案?

I need to convert objects to a byte[] to be stored in the Tokyo Cabinet key-value store.
I also need to unbyte the byte[] to an Object when reading from the key-value store.

Are there any packages out there that will help me with this task? Or would the best solution to implement it myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

随波逐流 2024-09-26 16:26:24
public static byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(out);
    os.writeObject(obj);
    return out.toByteArray();
}
public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    ObjectInputStream is = new ObjectInputStream(in);
    return is.readObject();
}
public static byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(out);
    os.writeObject(obj);
    return out.toByteArray();
}
public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
    ByteArrayInputStream in = new ByteArrayInputStream(data);
    ObjectInputStream is = new ObjectInputStream(in);
    return is.readObject();
}
东北女汉子 2024-09-26 16:26:24

如果您的类扩展了 Serialized,您可以通过 ByteArrayOutputStream 写入和读取对象,这就是我通常所做的。

If your class extends Serializable, you can write and read objects through a ByteArrayOutputStream, that's what I usually do.

半山落雨半山空 2024-09-26 16:26:24

使用 commons-lang

Use serialize and deserialize methods in SerializationUtils from commons-lang.

奢望 2024-09-26 16:26:24

您可以使用对象映射器

        ObjectMapper objectMapper = new ObjectMapper();
        ObjectClass object = objectMapper.readValue(data, ObjectClass.class);

You can use ObjectMapper

        ObjectMapper objectMapper = new ObjectMapper();
        ObjectClass object = objectMapper.readValue(data, ObjectClass.class);
随心而道 2024-09-26 16:26:24

您可以看看 Hector 如何为 Cassandra 执行此操作,其目标是相同的 - 将所有内容与 byte[] 相互转换,以便从 NoSQL 数据库存储/检索 - 参见此处。对于基本类型(+String),有特殊的序列化器,否则有通用的ObjectSerializer(期望Serialized,并使用ObjectOutputStream)。当然,您可以仅将其用于所有内容,但序列化形式中可能存在冗余元数据。

我想你可以复制整个包并使用它。

You can look at how Hector does this for Cassandra, where the goal is the same - convert everything to and from byte[] in order to store/retrieve from a NoSQL database - see here. For the primitive types (+String), there are special Serializers, otherwise there is the generic ObjectSerializer (expecting Serializable, and using ObjectOutputStream). You can, of course, use only it for everything, but there might be redundant meta-data in the serialized form.

I guess you can copy the entire package and make use of it.

新雨望断虹 2024-09-26 16:26:24

如果您不想序列化,可以使用对象映射器

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());

ByteArrayOutputStream out = new ByteArrayOutputStream();
objectMapper.writeValue(out,obj);
byte[] data = out.toByteArray();

我们应该何时实现可序列化接口?

If you do not want to serialize you can use object mapper

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());

ByteArrayOutputStream out = new ByteArrayOutputStream();
objectMapper.writeValue(out,obj);
byte[] data = out.toByteArray();

When should we implement Serializable interface?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文