Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

to serialize or not to serialize  RSS feed

 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

Im currently designing, and will then be coding, an address book program that stores and displays contact information (e.g. names, addresses, email addresses, etc).

Im trying to decide on whether to serialize an Contact object or just write the data to an .xml file.

Which approach is better? Is one prefered over the other? Is one method better/more efficient/or easier to search from over the other? I guess it's almost like a faux database.

Any help/suggestions are greatly appreciated.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Which approach is better?


That depends on how the saved data will be accessed...

If the data could be accessed by non-Java programs, the data needs to be human readable/editable, or will need to be queried/searched, use XML. (A relational database is also an option.)

If the data will be written/read only by your program, then use serialization. Serialized objects will also load faster than parsing XML.

If you use serialization, you should consider the implications class changes on deserialization. This article has a good discussion on that topic.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of the lock-in factor, I'm not too fond of using serialization for long-term data storage. For short-term storage (i.e., passivating sessions in an application server) or transmission (i.e., in remote method invocation) it's great; but for data you'd like to store for a long time, it's not very friendly to the user.
 
Brian LaRue
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i would eventually like to be able to query/search the collection. i would also like the program, when entering a new contact, to search the existing file/database to see if it already exists before adding/appending to the collection. So sounds like maybe .xml is the way to go then? I've never done .xml parsing, is there a good tutorial/book on the subject that you can recommend?
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you'll probably want to use XML instead of serialization.

Sun's XML Tutorial may be a good place to start.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!