I've got a certain POJO which must be endured on the database, current design identifies its area like a single string column, and adding additional fields towards the table isn't a choice.

Meaning, the objects have to be serialized in some manner. So only for the fundamental implementation I went and designed my very own serialized type of the item which meant concatenating all it's fields into one nice string, separated with a delimiter I selected. But this really is rather ugly, and may create problems, say if among the fields consists of my delimiter.

And So I attempted fundamental Java serialization, but from the fundamental test I carried out, this in some way turns into a very pricey operation (creating a ByteArrayOutputStream, an ObjectOutputStream, and so forth, same for that deserialization).

What exactly are my options? What's the preferred method for serializing objects to take a database?

Edit: this will probably be one such operation during my project, so overhead should be stored low, and gratifaction is vital. Also, third-party solutions are nice, but irrelevant (in most cases generate overhead that we am attempting to avoid)

Elliot Rusty Harold authored up a nice argument against using Java Object serialization for that objects in the XOM library. Exactly the same concepts affect you. The built-in Java serialization is Java-specific, fragile, and slow, and thus is better prevented.

You've roughly the best idea in making use of a String-based format. The issue, while you condition, is the fact that you are having formatting/syntax issues with delimiters. The answer is by using a format that's already built additional. If this sounds like a standardized format, you'll be able to also potentially use other libraries/languages to control it. Also, a string-based format means that you've a hope of understanding it simply by eyeballing the information binary formats remove that option.

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

You could try Protocol Buffers, it's a open-source project from Google, it's stated to become fast (creates shorter serialized form than XML, and works faster). Additionally, it handles addition of recent area lightly (card inserts default values).

You have to consider versioning inside your solution. Data incompatibility is a concern you'll knowledge about any solution which involves using a binary serialization from the Object. How can you load a mature row of information right into a more recent version from the object?

So, the solutions above which entail serializing to some title/value pairs may be the approach you most likely desire to use.

One option would be to incorporate a version number among area values. As new fields are added, modified or removed then your version could be modified.

When deserializing the information, you could have different deserialization handlers for every version which may be used to convert data in one version to a different.

Consider putting the information inside a Properties object and employ its load()/store() serialization. This is a text-based technique therefore it is still readable within the database:

public String getFieldsAsString() {
  Properties data = new Properties();
  data.setProperty( "foo", this.getFoo() );
  data.setProperty( "bar", this.getBar() );
  ...
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  data.store( out, "" );
  return new String( out.toByteArray(), "8859-1" );   //store() always uses this encoding
}

To load from string, do similar utilizing a new Properties object and load() the information.

This is preferable to Java serialization since it is very readable little.

If you want support for various data types (i.e. not only String), use BeanUtils to transform each area back and forth from a string representation.

XStream or YAML or OGNL spring to mind as simple serialization techniques. XML continues to be the most typical, but OGNL offers the most versatility using the least quantity of metadata.