What is the best alternative to Java Serialization?

I am currently working on a project that must support an object (from which we have no control) so that later these objects can be restored.

We cannot implement ORM because we cannot restrict the users of our library during development.

Our first alternative was to serialize it with default serialization of Java, but we had a lot of problems with restoring objects when users started passing different versions of the same object (attributes changed types, names, etc.).

We tried with the XMLEncoder class (we convert the object to XML), but we found that there is a lack of functionality (for example, it does not support Enums).

Finally, we also tried JAXB, but it forces our users to annotate their classes.

Any good alternative?

+41
java serialization xml-serialization
Oct 27 '08 at 7:41
source share
12 answers

The simplest thing for you is to use serialization, IMO, but get more into the serialized form of classes (which you really should do anyway). For example:

  • Explicitly determine SerialUID.
  • Define your own serialized form, if necessary.

The serialized form is part of the API class, and careful thinking should be included in its design.

I will not go into details because almost everything I said comes from Effective Java. Instead, I will tell you about this, namely the chapters on serialization. He warns you about all the problems that you have encountered, and offers the correct solutions to the problem:

http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683




With that said, if you are still considering a non-serialization approach, here are a couple:

XML sorting

As many have pointed out, this is an option, but I think that you will still encounter the same backward compatibility issues. However, with XML sorting, you are likely to understand them right away, as some frameworks may perform some checks for you during initialization.

Convert to / from YAML

This is an idea I played with, but I really liked the YAML format (at least as the custom toString () format). But in fact, the only difference for you is that you have to sort YAML instead of XML. The only advantage is that YAML is a bit more readable than XML. The same restrictions apply.

+9
Oct 27 '08 at 12:23
source share

In 2011, and in the REST commercial web services project, we use the following serializers, offering customers various types of media:

  • XStream (for XML, but not for JSON)
  • Jackson (for JSON)
  • Kryo (fast, compact binary serialization format)
  • Smile (the binary format that ships with Jackson 1.6 and later).
  • Serialization of Java objects.

We recently experimented with other serializers:

  • SimpleXML seems solid, runs at 2x XStream, but our configuration requires too much configuration.
  • YamlBeans had a couple of errors.
  • SnakeYAML had a minor date error.

Jackson JSON, Kryo, and Jackson Smile were significantly faster than the old old Java Object serializations, from about 3x to 4.5x. XStream is on the slow side. But this time, all this is a solid choice. We will follow the other three.

+30
Mar 14 2018-11-11T00:
source share

http://x-stream.imtqy.com/ nice, please take a look at it! Very comfortably

+18
Oct 27 '08 at 7:43
source share

in the implementation of which we have no control

The solution does not do this. If you do not control the type implementation, you should not serialize it. The end of the story. Java serialization provides a serialVersionUID specifically for managing serialization incompatibilities between different versions of a type. If you do not control the implementation, you cannot be sure that the identifiers change correctly when the developer changes the class.

Let's take a simple example of a “Point”. It can be represented either by the Cartesian or the polar coordinate system. It would be expensive for you to create a system that could dynamically deal with such corrections - it really should be the developer of the class that designs serialization.

In short, this is your design that is wrong - not technology.

+15
Oct 27 '08 at 12:39
source share

Google came up with a binary protocol - http://code.google.com/apis/protocolbuffers/ faster, has less payload compared to XML - which others suggested as an alternative.

One of the advantages of protocol buffers is that it can exchange information with C, C ++, python and java.

+7
Oct 28 '08 at 2:46
source share

Try serializing for json using Gson .

+5
Oct 27 '08 at 7:45
source share

Also a very quick JDK serialization replacement: http://ruedigermoeller.imtqy.com/fast-serialization/

+5
Dec 6
source share

If serialization speed is important to you, then there is a comprehensive standard for JVM serializers:

+2
Jul 16 '13 at 11:53 on
source share

Maybe Castor ?

+1
Oct 27 '08 at 11:15
source share

Personally, I often use Fame , as it has interoperability with Smalltalk (both VW and Squeak) and Python. (Disclaimer, I am a major contributor to the Fame project.)

+1
Oct 27 '08 at 12:39
source share

Betwixt is a good library for serializing objects - but it will not be an automatic kind of thing. If the number of objects you need to serialize is relatively fixed, this may be a good option for you, but if your “client” constantly throws you new classes, it may be more effort than it costs (Definitely easier than XMLEncoder for everyone special cases though).

Another approach is to require your client to provide the appropriate .betwixt files for any objects that they throw at you (which effectively offloads the responsibility to them). A.

Long and short serialization is tough - there is absolutely no dead approach to it. Java serialization is as close to a dead brain solution as I have ever seen, but as you have discovered, misuse of the uid version value can break it. Java serialization also requires the use of a “Serializable” token interface, so if you cannot control your source, you are out of luck.

If the requirement is really as complicated as you describe, you may need some kind of BCE (modification of the byte code) for objects / aspects / independently. This goes far beyond a small development project, and into the realm of Hibernate, Casper or ORM ....

+1
Oct 28 '08 at 2:34
source share

Another idea: use a cache. Caches provide much better control, scalability and reliability of the application. However, you still need to serialize, but management becomes much simpler within the caching framework. A cache can be stored in memory, disk, database or array - or all options - with one overflow, wait, crash for another. Commons JCS and Ehcache are two Java implementations, the latter is an enterprise solution, free of storage up to 32 GB (disclaimer: I do not work for ehcache ;-)).

0
Aug 10 '13 at
source share



All Articles