简体   繁体   English

从磁盘读取序列化的Java对象,使用较新的类?

[英]Read serialized java object from disk with newer class?

Just a simple question: 只是一个简单的问题:

If I saved a java object on disk, but after that I added some more data member (String type), can new class read those old java object back with those new String member as null? 如果我在磁盘上保存了一个Java对象,但是之后又添加了一些数据成员(字符串类型),那么新类可以将那些新的String成员作为null读回那些旧的Java对象吗?

This is possible, but for that you need to manage compatibility actively. 这是可能的,但是为此您需要主动管理兼容性。 You need to ensure that the new class has the same serialVersionUID value as the old class. 您需要确保新类具有与旧类相同的serialVersionUID值。 This private static final long serialVersionUID field can be auto-generated - or manually assigned. private static final long serialVersionUID字段可以自动生成-或手动分配。 See http://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html 参见http://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html

If you already have a serialVersionUID , the new field will be null. 如果您已经有serialVersionUID ,则新字段将为null。

If you don't, you want to create one and use the same value as the auto-generated value of the old class. 如果不这样做,则要创建一个,并使用与旧类的自动生成的值相同的值。 That's what the tool serialver is for. 这就是serialver工具的用途。

The new String member will be whatever the default value is in the class definition I believe (or set in a default constructor). 我相信(或在默认构造函数中设置)类定义中的默认值将是新的String成员。

Do you have to use ObjectOutputStream / ObjectInputStream to serialize your Java objects? 您是否必须使用ObjectOutputStream / ObjectInputStream序列化Java对象? Those are 'old-school' and write binary (not human readable format). 这些都是“老派”,并写二进制(不是人类可读的格式)。 This format retains actual structural (field) knowledge of the class. 这种格式保留了该类的实际结构(现场)知识。

If you used JSON for serialization (or technically XML but it will be larger and harder to deal with), the serialized content will be more 'future-proof'. 如果您使用JSON进行序列化(或从技术上说是XML,但会更大且更难处理),则序列化的内容将更具“面向未来”的能力。 When reading an old JSON String into Java, if it has missing fields for a newer version of the class, the fields will retain their default values. 当将旧的JSON字符串读取到Java中时,如果该类的较新版本缺少缺少的字段,则这些字段将保留其默认值。 Most importantly, the serialized data will be human-readable, and easily edited with any modern IDE or a website like: http://www.jsoneditoronline.org/ 最重要的是,序列化的数据将是人类可读的,并且可以通过任何现代IDE或类似http://www.jsoneditoronline.org/的网站轻松进行编辑

If you can, try a Java to JSON (and JSON to Java) serialization library like json-io ( https://github.com/jdereg/json-io ). 如果可以,请尝试使用Java到JSON(以及JSON到Java)的序列化库,例如json-io( https://github.com/jdereg/json-io )。 With just one line of code, you can convert your Java object graph into a JSON String: 仅需一行代码,您就可以将Java对象图转换为JSON字符串:

String json = JsonWriter.objectToJson(root);

Similarly, with one line of code, you can read this JSON back into Java objects: 同样,只需一行代码,您就可以将该JSON读回到Java对象中:

Object root = JsonReader.jsonToJava(json);

Finally, using JSON format works great with Javascript. 最后,使用JSON格式可与Javascript完美配合。 If a webserver returns an HTTP response with JSON content, this content will make it's way to the Javascript developer as objects, ready-to-go (the Javascript developer will not have to write parsing code). 如果网络服务器返回带有JSON内容的HTTP响应,则该内容将使其成为Java开发人员可以随时使用的对象(Java开发人员不必编写解析代码)。 The objects will be straight-up readable as if they were defined in the Javascript code. 这些对象将是直接可读的,就像它们是在Javascript代码中定义的一样。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM