• Post Reply Bookmark Topic Watch Topic
  • New Topic

examples of when to use serialization  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think i understand the basic concept, but at first i didnt. in searching this site i saw others who were a bit confused about this. i have one example.

say you wrote a game and all you need to do, is at the end update the top scores. then just writing name and score to a file is quite simple.
but, suppose instead you want to save the game. now we are talking about hundreds of variables. creating a format to save the file and then have to parse the file and assign the values to the variables would be a nightmare of grunt work, and prone to errors as well.

any other examples would help both me and others who havent had to do this before.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also serialise the game to a byte[] and send across the globe to your opponent across the street.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that "serialization" is a generic term which refers to converting between an internal (Java objects) format and and external (bytes on the wire or in a file) format.

So don't assume that "serialization" means you have to use ObjectOutputStream and all of that sort of thing. There is also java.beans.XMLEncoder which can be used to serialize an object to XML, for example.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but, suppose instead you want to save the game. now we are talking about hundreds of variables. creating a format to save the file and then have to parse the file and assign the values to the variables would be a nightmare of grunt work, and prone to errors as well.


Or - code a single object "StateOfTheGame" which holds all these variables and is Serializable. No parsing, variables already have the state.

Don't be afraid of Serialize-ing a complex object - it works great and can be quite compact and fast. StateOfTheGame can contain collections and other complex objects as long as they are also Serializable.

Bill
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization means converting a Java object that's in memory to bytes (binary data) so that you can store it (to be loaded again later) or to transmit it over the network.

In the project that I'm currently working on, we are building a system that can work in a clustered way - there can be multiple servers in the system that work together. Sometimes these servers need to share data, and we do that by serializing objects containing the data, sending the bytes to the other server over the network, and then deserializing the bytes back into a Java object on the other server.

Note that the standard Java serialization mechanism is not well suited for long-term storage of objects. Don't write a program in which the user's data is stored in files by using Java's built-in serialization mechanism. The problem is that the serialization mechanism very tightly binds your source code to the bytes that are stored in the file - if you change something to your classes in a newer version of your program, then it will not be able to load old files anymore. Also, the exact format of serialized data is not easy to understand (I guess the specifications are available somewhere, but it's not a well-known standard format). For such applications, it's much better to use a standard format (XML for example, or any other standard format).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!