简体   繁体   English

使用序列化是个好主意吗?

[英]Is using Serialization a good idea?

There are several risks regarding Serialization including incompatible changes. 序列化存在一些风险,包括不兼容的更改。 If incompatible changes occur in the classes being serialized then we can't de-serialize it even with static final long serialVersionUID field. 如果要序列化的类中发生不兼容的更改,那么即使使用静态final final serialVersionUID字段,我们也无法对其进行反序列化。

So, what's the alternatives of serialization ? 那么,序列化的替代方案是什么? XML ? XML? If there's any alternative then is there at all any use of serialization in real world projects ? 如果有任何替代方案,那么在现实世界项目中是否完全使用序列化?

Sure there are alternatives to Java serialization: XML (as you've noted); 当然还有Java序列化的替代品:XML(正如你所注意到的); JSON; JSON; protobuf; protobuf的; anything else that you'd care to use instead. 你想要使用的任何其他东西。

All of them will run some risk of incompatible changes. 所有这些都会带来一些不兼容的变化风险。 I don't see that there's any magic in the other methods. 我没有看到其他方法中有任何魔力。 If you add a new attribute to an object, you've got to deal with "duck typing". 如果向对象添加新属性,则必须处理“duck typing”。 If you remove an attribute that's required, all methods will have problems. 如果删除所需的属性,则所有方法都会出现问题。

you seem to be confusing serialization with data formats - it's probably best to separate them. 您似乎将序列化与数据格式混淆 - 最好将它们分开。

there are, broadly, three kinds of serialization: 大致有三种序列化:

  1. automated, comprehensive, low-level - this is useful because it can be provided as a service and requires little effort to use. 自动化,全面,低级 - 这很有用,因为它可以作为服务提供,并且只需要很少的工作量。 but, by its nature, it is closely related to the internal format used in the language/program - if that changes then de-serialization will be "difficult". 但是,就其性质而言,它与语言/程序中使用的内部格式密切相关 - 如果这种情况发生变化,那么反序列化将是“困难的”。 typical examples are the "native" serialization provide by java and python; 典型的例子是java和python提供的“本机”序列化; it's useful for short-term snapshots of program state. 它对程序状态的短期快照很有用。

  2. automated, restricted, medium-level - typically used for communication/inter-operation. 自动,受限制,中等级 - 通常用于通信/互操作。 this will not support all features of a language, probably allowing only simple data structures to be saved. 这不支持语言的所有功能,可能只允许保存简单的数据结构。 if that's sufficient then it's a useful middle ground as it can be automated but is independent of the language's internal format (but not, necessarily, of the program's details). 如果这是足够的,那么它是一个有用的中间立场,因为它可以自动化,但独立于语言的内部格式(但不一定是程序的细节)。 typical examples include protocol buffers, thrift, and json libraries ( not json itself, which is a data format...); 典型的例子包括协议缓冲区,thrift和json库( 不是 json本身,它是一种数据格式......); obviously, this is useful for comms and as ad-hoc data stores. 显然,这对于通信和ad-hoc数据存储非常有用。

  3. "hand coded", high level - this requires more work, and will probably save less information, but aims to extract the "important parts" in a way that is independent of the internal format used in the program (or language). “手工编码”,高级 - 这需要更多的工作,并且可能会节省更少的信息,但旨在以独立于程序(或语言)中使用的内部格式的方式提取“重要部分”。 examples include docbook or open document (jaxb is rather nice in providing support for close-to-(1) by default, but allowing close-to-(3) with care); 例子包括docbook或open document(jaxb在默认情况下提供对close-to-(1)的支持相当不错,但是要小心接近(3)); this is useful for long-term data archiving and is often associated with "official" specs. 这对于长期数据存档很有用,并且通常与“官方”规范相关联。

separate from the above is the data format used to encode the serialized data. 与上面分开的是用于编码序列化数据的数据格式。 i am sure you can implement all the above in xml; 我相信你可以在xml中实现上述所有功能; json tends to be used at (2). json倾向于在(2)中使用。

anyway, to answer the question - (1) has the problems you note. 无论如何,要回答这个问题 - (1)有你注意到的问题。 if (2) is sufficient, then it is a simple solution (but still suffers from some of the problems of (1) if data structures within the program change); if(2)就足够了,那么这是一个简单的解决方案(但是如果程序中的数据结构发生变化,仍会遇到(1)的一些问题); otherwise you need to do the extra work implied by (3). 否则你需要做(3)所暗示的额外工作。

The answer depends on your goal. 答案取决于你的目标。 If you're sending data between Java processes, the default Java serialization mechanism may work fine. 如果您在Java进程之间发送数据,则默认的Java序列化机制可能正常工作。

However, for storing data, particularly data which humans may want to look at or edit, the Java serialization mechanism is poorly suited. 但是,为了存储数据,特别是人们可能想要查看或编辑的数据,Java序列化机制非常不适合。

There are a number of great XML-based java serialization libraries which I greatly prefer when human readable/editable output is required. 有许多优秀的基于XML的java序列化库,当我需要人类可读/可编辑的输出时,我更喜欢这些库。

Two great ones are: XStream and JAXB 两个很棒的是: XStreamJAXB

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

相关问题 正在使用List <Byte> 不是好主意? - Is using List<Byte> not good idea? 在外部API中使用已检查的异常是一个好主意吗? - Is using checked exceptions in external APIs a good idea? 一起使用HtmlCleaner和Jsoup是一个好主意吗? - Is using HtmlCleaner and Jsoup together a good idea? 使用 Spring AOP 进行日志记录是个好主意吗? - Is using Spring AOP for logging a good idea? 何时将JDBC与Lucene索引一起使用是个好主意? - when is using JDBC with lucene indexes a good idea? 使用Proxy模式编写服务器是个好主意? - Using Proxy pattern to write a server a good idea? 使用序列化将数据存储在单个文件中是一个坏主意吗? - Is using serialization to store data in a single file a bad idea? 在 Java 中使用序列化实现克隆方法是一种好习惯吗? - Is it good practice in Java to implement the clone method using serialization? 使用 datastax 映射器使用 saveAsync 保存数千条记录是个好主意吗 - Is it a good idea to use datastax mapper to save thousands of records using saveAsync 在Java中使用省略号表示可选参数是一个好主意吗? - In Java is using the ellipsis to represent an optional parameter a good idea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM