views:

129

answers:

4

I have a large data-structure which i'm serializing.At certain times i need to edit the values in the data-structure.But just for changing a small value i'll have to re-serialize it again instead of updating the changed value in file.I've heard of Google protocol buffer's.Will using it solve my problem of rewriting the file ? Is it a better option for me to use protocol buffer instead of Java serialization ?

+5  A: 

Protocol buffers are themselves a serialization format, so they won't fundamentally change the picture (you'll still need to re-serialize after you change a value).

Google's docs claim that protocol buffers are more compact and faster to parse than XML (which seems plausible); don't know how they compare to native Java serialization.

Advantages of protocol buffers might be portability (if programs written in other languages need to read the file) and upgradability (you can add new fields to the data structure without breaking the file format).

David Gelhar
A: 

You need a serialization format that can directly be modified for example XML or JSON. Google protocol buffer is a binary format -- as the java serialization -- and thus can not be modifier directly...

pgras
+3  A: 

A couple of points

  1. There is an editor for Protocol Buffers binary format (http://code.google.com/p/protobufeditor/)
  2. Protocol buffers has a text format that looks like:
# Textual representation of a protocol buffer.
# This is *not* the binary format used on the wire.
person {
  name: "John Doe"
  email: "[email protected]"
}

See:

Having said that, I would use a technology (JSon, Xml etc) that is already in use unless one of the following applies

  1. You need the performance of protocol buffers
  2. You already / plan to use protocol buffers
Bruce Martin
+3  A: 

If you care about performance, don't use a text format for your data. If you want to modify the data without deserializing, you'll want to use a fixed record data format. You'll probably have to invent this manually. Then seek to the correct position in the file and rewrite just the changed field. You might look at DataOutputStream to get started or instead use a database such as HSQLDB to store and edit your data.

Thinking about this more, Unless your objects are very simple, I think a database would be a better way to go.

More info on DataOutputStream: http://download.oracle.com/javase/tutorial/essential/io/datastreams.html

Java Databases: http://java-source.net/open-source/database-engines

Joshua Martell
can you give a code sample.
Emil