自制的与Java的序列化[英] Homemade vs. Java Serialization

本文是小编为大家收集整理的关于自制的与Java的序列化的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个需要在数据库上持续存在的POJO,当前设计将其字段指定为单字符串列,并且在表中添加其他字段不是一个选项.

的意思是,需要以某种方式序列化对象.因此,仅对于基本实现,我就设计了自己的对象的序列化形式,该形式意味着将所有字段串联成一个很好的字符串,并由我选择的定界符分隔.但这很丑陋,可能会引起问题,说其中一个字段是否包含我的定界符.

因此,我尝试了基本的Java序列化,但是从我进行的基本测试中,这是一个非常昂贵的操作(构建bytearrayoutputstream,objectOutputstream,or of of of of of of of of of the delesialization).

).

).

那我有什么选择?序列化对象在数据库上的首选方法是什么?

编辑:这将是我项目中非常普遍的操作,因此开销必须保持在最低限度,并且性能至关重要.另外,第三方解决方案很好,但无关紧要(通常会产生我试图避免的开销)

推荐答案

Elliot Rusty Harold写下了不错的参数他的XOM库中的对象.相同的原则适用于您.内置的Java序列化是Java特异性的,脆弱的和缓慢的,因此最好避免.

您使用基于字符串的格式有大致正确的想法.如您所述,问题在于,您正在遇到分界符的格式/语法问题.解决方案是使用已构建的格式来处理此问题.如果这是标准化的格式,那么您也可以使用其他库/语言来操纵它.另外,基于字符串的格式意味着您只能通过浏览数据来理解它.二进制格式删除该选项.

XML和JSON在这里是两个不错的选择.它们是标准化,基于文本的,灵活的,可读的,并且具有大量的图书馆支持.它们的表现也出奇地表现出色(有时甚至比Java序列化快).

其他推荐答案

您可以尝试协议缓冲器,这是一个开放式项目Google,据说它很快(生成比XML较短的序列化表单,并且工作速度更快).它还可以轻轻处理新的字段的添加(插入默认值).

其他推荐答案

您需要考虑在解决方案中进行版本化.数据不兼容是您将在任何涉及使用对象二进制序列化的解决方案中遇到的问题.如何将旧数据加载到对象的较新版本中?

因此,涉及序列化到名称/值对的上述解决方案是您可能想要使用的方法.

一种解决方案是将版本编号作为字段值之一.随着新字段的添加,修改或删除,可以修改版本.

避免数据时,您可以为每个版本具有不同的供应处理程序,可用于将数据从一个版本转换为另一个版本.

本文地址:https://www.itbaoku.cn/post/597642.html

问题描述

I have a certain POJO which needs to be persisted on a database, current design specifies its field as a single string column, and adding additional fields to the table is not an option.

Meaning, the objects need to be serialized in some way. So just for the basic implementation I went and designed my own serialized form of the object which meant concatenating all it's fields into one nice string, separated by a delimiter I chose. But this is rather ugly, and can cause problems, say if one of the fields contains my delimiter.

So I tried basic Java serialization, but from a basic test I conducted, this somehow becomes a very costly operation (building a ByteArrayOutputStream, an ObjectOutputStream, and so on, same for the deserialization).

So what are my options? What is the preferred way for serializing objects to go on a database?

Edit: this is going to be a very common operation in my project, so overhead must be kept to a minimum, and performance is crucial. Also, third-party solutions are nice, but irrelevant (and usually generate overhead which I am trying to avoid)

推荐答案

Elliot Rusty Harold wrote up a nice argument against using Java Object serialization for the objects in his XOM library. The same principles apply to you. The built-in Java serialization is Java-specific, fragile, and slow, and so is best avoided.

You have roughly the right idea in using a String-based format. The problem, as you state, is that you're running into formatting/syntax problems with delimiters. The solution is to use a format that is already built to handle this. If this is a standardized format, then you can also potentially use other libraries/languages to manipulate it. Also, a string-based format means that you have a hope of understanding it just by eyeballing the data; binary formats remove that option.

XML and JSON are two great options here; they're standardized, text-based, flexible, readable, and have lots of library support. They'll also perform surprisingly well (sometimes even faster than Java serialization).

其他推荐答案

You might try Protocol Buffers, it is a open-source project from Google, it is said to be fast (generates shorter serialized form than XML, and works faster). It also handles addition of new field gently (inserts default values).

其他推荐答案

You need to consider versioning in your solution. Data incompatibility is a problem you will experience with any solution that involves the use of a binary serialization of the Object. How do you load an older row of data into a newer version of the object?

So, the solutions above which involve serializing to a name/value pairs is the approach you probably want to use.

One solution is to include a version number as one of field values. As new fields are added, modified or removed then the version can be modified.

When deserializing the data, you can have different deserialization handlers for each version which can be used to convert data from one version to another.