Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By ras_oscar

Thread: Fundamental question regarding object serialization

  1. #1
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Fundamental question regarding object serialization

    I have written several simple data classes that I serialized manually by converting to text. At this point I need to serialize a more complex data structure. which will include lists of the simpler elements. Can serialize store and reconstitute this type of structure automatically or do I need to do this one manually as well? Consider the pseudocode below for a clarification of my question;

    Java Code:
    Class Hops{
             String Name;
             float Alpha Acid;
             float Beta Acid;
    };
    
    Class Malt{
            String Name;
            float extract;
    };
    
    Class Recipie{
            String Name;
            CList HopList;
            CList MaltList;
    };     
    
    CList RecipieList;




    I read the article on Serialization presented at tutorial point, but the example only showed a simple class, not lists of class members. What I want to do is serialize RecipieList, which consists of a CList of Recipies, which in turn consist of CLists of various ingredients.
    Last edited by ras_oscar; 10-06-2014 at 06:30 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Fundamental question regarding object serialization

    What happened when you tried?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Fundamental question regarding object serialization

    The serialization mechanism is quite clever and it can do what you have in mind (you don't have to do anything for it, except implementing the (empty) Serializable interface for your classes).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: Fundamental question regarding object serialization

    Ok, thanks. I had set this project aside for several months, secretly dreading the thought of manual serializating this mess. I stumbled upon serialize by accident while looking for something else. I'll give it a try. Who knows, I may even re-write my other serialization code!!!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Fundamental question regarding object serialization

    Quote Originally Posted by ras_oscar View Post
    Who knows, I may even re-write my other serialization code!!!
    You don't have to write any code for your classes to be serializable; just make them implement the (empty) Serializable marker interface.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Fundamental question regarding object serialization

    You can always use the default implementation. But there is nothing to prevent you from writing your own. For instance
    you could feasibly write serialization routines to encode your objects in XML if you wanted.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Fundamental question regarding object serialization

    Note that serialization is a bit more than finding a fancy representation for objects; circular references (object A refers to object B and object B refers to object A) need to be handled, otherwise your serialization process will eat all the stack space or worse ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Fundamental question regarding object serialization

    Quote Originally Posted by JosAH View Post
    ... otherwise your serialization process will eat all the stack space or worse ...
    ...it will eat...The WORLD!
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Fundamental question regarding object serialization

    Quote Originally Posted by Tolls View Post
    ...it will eat...The WORLD!
    Or worse: the entire universe and everything! *shudder*

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Fundamental question regarding object serialization

    Quote Originally Posted by Tolls View Post
    ...it will eat...The WORLD!
    See also: Grey goo - Wikipedia, the free encyclopedia
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  11. #11
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: Fundamental question regarding object serialization

    no worries regarding circular references, it s a leaf/branch/tree/forest relationship.

  12. #12
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: Fundamental question regarding object serialization

    Wrote a simple self contained program to write and then read an ArrayList of strings to a file, offering the user a dialog box to add a unique string as well (to verify unique runs of the program). It worked!!! Now to complete adding serealization to my application. No questions, just sharing. Does anyone want to see the test program?
    DarrylBurke likes this.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Fundamental question regarding object serialization

    Quote Originally Posted by ras_oscar View Post
    Wrote a simple self contained program to write and then read an ArrayList of strings to a file, offering the user a dialog box to add a unique string as well (to verify unique runs of the program). It worked!!! Now to complete adding serealization to my application. No questions, just sharing. Does anyone want to see the test program?
    Sure, why not? It'd be nice for people stumbling on this thread through google, so they'd find a nice answer; beware though, if your solution contains errors, be prepared to be flamed or made fun at ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  14. #14
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: Fundamental question regarding object serialization

    Yeah, What's the worst that could happen? I spark a lively debate on my shortcomings and we all learn something. Here ya go. I even remember the code tags and I personally adjusted the indentations :)

    Java Code:
    package test;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author ras_oscar
     */
    public class Test 
        {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
            {
            //this file tests the serealization functions
            ArrayList <String> stringlist=new ArrayList<>();   
            String String1="this is the first string";
            String String2="this is the second string";
            String String3="this is the third string";
            boolean ret=stringlist.add(String1);
            ret=stringlist.add(String2);
            ret=stringlist.add(String3);
            
            // get a string from the user in a dialog box
                  String s = (String)JOptionPane.showInputDialog(
                  null,
                  "Enter a string to add",
                  "String Input Dialog",
                  JOptionPane.PLAIN_MESSAGE,
                  null,null,null);
                  ret=stringlist.add(s);
               //-----------------we now have stringlist conaining 4 string elements
          try
          {
             FileOutputStream fileOut =
             new FileOutputStream("c:/beer/recipie.bdb");
             ObjectOutputStream out = new ObjectOutputStream(fileOut);
             out.writeObject(stringlist);
             out.close();
             fileOut.close();
             System.out.printf("Serialized data is saved in c:/beer/recipie.bdb");
          }catch(IOException i)
            {
              i.printStackTrace();
            }
    //arraylist stringlist now serialized as recipie.bdb. Now lets get it back
            try
            {
               FileInputStream fileIn = new FileInputStream("c:/beer/recipie.bdb");
               ObjectInputStream in = new ObjectInputStream(fileIn);
               stringlist = (ArrayList <String>) in.readObject();
               in.close();
               fileIn.close();
            }catch(IOException i)
            {
               i.printStackTrace();
               return;
            }catch(ClassNotFoundException c)
            {
               System.out.println("Test class not found");
               c.printStackTrace();
               return;
            }
            }
        }

Similar Threads

  1. Replies: 4
    Last Post: 03-22-2012, 04:26 PM
  2. Fundamental question about files
    By salmontres in forum New To Java
    Replies: 4
    Last Post: 12-08-2011, 01:14 AM
  3. Object Serialization
    By rdjava in forum Advanced Java
    Replies: 1
    Last Post: 06-12-2011, 09:54 AM
  4. fundamental question about Integer (for example)
    By kmansj in forum New To Java
    Replies: 10
    Last Post: 06-21-2010, 12:40 PM
  5. Object serialization
    By sky in forum New To Java
    Replies: 15
    Last Post: 11-23-2009, 01:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •