开发者

Serialization framework (no no-arg constructor)

I'm looking for some info on the best approach serialize a graph of object based on the following (Java):

  • Two objects of the same class must be binary equal (bit by bit) compared to true if their state is equal. (Must not depend on JVM field ordering).
  • Collections are only modeled with arrays (nothing Collections).
  • All instances are immutable
  • Serialization format should be in byte[] format instead of text based.
  • I am in control of all the classes in the graph.

I don't want to put an empty constructor in the classes just to support serialization. I have looked at implementing 开发者_JAVA百科a solution based my own traversal an on Objenisis but my problem does not seem that unique. Better checking for any existing/complete solution first.

Updated details:

First, thanks for your help!

  • Objects must serialize to exactly the same bit order based on the objects state. This is important since the binary content will be digitally signed. Reconstruction of the serialized format will be based on the state of the object and not that the original bits are stored.
  • Interoperability between different technologies is important. I do see the software running on ex. .Net in the future. No Java flavour in the serialized format.

Note on comments of immutability: The values of the arrays are copied from the argument to the inner fields in the constructor. Less important.

Best regards,

Niclas Lindberg


You could write the data yourself, using reflections or hand coded methods. I use methods which are look hand code, except they are generated. (The performance of hand coded, and the convience of not having to rewrite the code when it changes)

Often developers talk about the builtin java serialization, but you can have a custom serialization to do whatever you want, any way you want.

To give you are more detailed answer, it would depend on what you want to do exactly.

BTW: You can serialize your data into byte[] and still make it human readable/text like/editable in a text editor. All you have to do is use a binary format which looks like text. ;)


Maybe you want to familiarize yourself with the serialization frameworks available for Java. A good starting point for that is the thift-protobuf-compare project, whose name is misleading: It compares the performance of more than 10 ways of serializing data using Java.

It seems that the hardest constraint you have is Interoperability between different technologies. I know that Googles Protobuffers and Thrift deliver here. Avro might also fit.


The important thing to know about serialization is that it is not guaranteed to be consistent across multiple versions of Java. It's not meant as a way to store data on a disk or anywhere permanent.

It's used internally to send classes from one JVM to another during RMI or some other network protocol. These are the types of applications that you should use Serialization for. If this describes your problem - short term communication between two different JVM's - then you should try to get Serialization going.

If you're looking for a way to store the data more permanently or you will need the data to survive in forward versions of Java, then you should find your own solution. Given your requirements, you should create some sort of method of converting each object into a byte stream yourself and reading it back into objects. You will then be responsible for making sure the format is forward compatible with future objects and features.

I highly recommend Chapter 11 of Effective Java by Joshua Bloch.


Is the Externalizable interface what you're looking for ? You fully control the way your objects are persisted and you do that the OO-style, with methods that are inherited and all (unlike the private read-/write-Object methods used with Serializable). But still, you cannot get rid of the no-arg accessible constructor requirement.


The only way you would get this is: A/ USE UTF8 text, I.E. XML or JSON, binary turned to base64(http/xml safe variety). B/ Enforce UTF8 binary ordering of all data. C/ Pack the contents except all unescaped white space. D/ Hash the content and provide that hash in a positionally standard location in the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜