Serializable, Externalizable interfaces in java

Discussion in 'OT Technology' started by D1G1T4L, Jan 12, 2003.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    i am learning how to use them right now, so from what i understand you use them to save an object's state and recover it later if you want.

    1st question. Is this used a lot? because from what i hear it is but i never seen any programs use these interfaces. Seems like it's pretty cool though

    2nd question i was wondering if c++, c#, vb and other languages have something like this?
     
  2. crotchfruit

    crotchfruit Guest

    the reason java has such well defined serialization is that for the most part it's an interpreted language. in other words, the virtual machine is able to dynamically 'know' everything about a specific class at any time.

    java serialization doesn't just take a memory snapshot of the class when it serializes, it makes a list of members and their values.

    one benefit of this is that if you ever make a change to a class (i.e. add a member), it's not going to screw up your java serialization. upon restore, it will go through the serialized data and restore the members it knows about, while ignoring those that have been added since the serialize.

    another benefit is that it can serialize object trees really nicely. for example, if you have a vector in your object that contains lots of other objects, they will all be serialized into one stream. it's handy :)

    the reason c++, for example, can't do this is because at runtime there is no way to access the "source code name" for a class's member. there is also no guarantee about how classes are organized in memory, so it's hard to make cross-platform serialization libs.

    to use serialization effectively, check out the java docs on the ObjectOutputStream and ObjectInputStream classes.

    :big grin:
     
  3. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    thx for that brief description, but you basically explained everything i learned and not what i asked (except the c++ part) heh but still good explanation for people who don't know what we are talking about :bigthumb:
     
  4. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    1st question. Is this used a lot? because from what i hear it is but i never seen any programs use these interfaces. Seems like it's pretty cool though

    2nd question i was wondering c#, vb.net have same thing cause they use runtime environment as well

    3rd question: what alternatives are there for C++ ? or how would a C++ programmer handle a situation where he needs to save an object with current state variables? (can save all the fields values in txt files but that doesnt seem professional to me?)
     
  5. AznRyda

    AznRyda キモかわいい!

    Joined:
    May 6, 2000
    Messages:
    28,632
    Likes Received:
    0
    Location:
    残業中
    I used serializable a lot, but I'm not in the industry. :o
     
  6. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    give me examples of what you use it for
     
  7. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
  8. crotchfruit

    crotchfruit Guest

    1) it's hard for anyone to answer this.. we don't have access to most 'industry' source code to see how they are saving their data structures, etc. i would imagine that for heavy-duty applications that store lots and lots of data, they would not use the java serialization interface, since it is relatively bloated (as opposed to raw binary serialization.)

    2) can't answer this

    3) i have seen some c++ serialization libs. i'm sure a google search will help here. the problem with c++ serialization libs is that they are obviously not as flexible/dynamic as the java interface (since they are compiled, and the memory manipulation of the serialization routines is set in stone... this means that to some degree there is little compatibility when you modify the class to be serialized.)

    Here's the deal:

    Java serialization = bloated, but fast, useful and flexible.

    C++ serialization = exists, but is not "native" and provides less support for importing serialized data of classes in which the structure has changed since the data was saved.
     
  9. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    thx

    but more info on number 3 would be great
     
  10. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The simple answer to #3 is that it's mostly do-it-yourself. Sometimes a class library may implement some form of serialization. But, if you're designing your own class hierarchy and want serialization, make provisions for it in your base classes, so serializable objects will have a common interface. And, there's nothing wrong with text files. Again, the format for persistent data is do-it-yourself (and it doesn't necessarily have to be file-based, you could be serializing data to be stored in a database). A good format choice would be XML. Given the nature of XML, it wouldn't be too difficult to implement some of the nice serialization features that crotchfruit mentioned.
     
  11. crotchfruit

    crotchfruit Guest

    one solution to c++ serialization is to use a hashset as the member variable holder, not the native c++ class implementation. of course this will bloat and slow your code, but if serialization is more important than speed, it might be for you.

    basically, instead of having individual member variabes, i.e (pseudo code):

    class myclass
    {
    public:
    int blah;
    int moo;
    int foo;
    };

    you would do:

    class myclass
    {
    public:
    hashset myvars;
    }

    the hashset 'key' would be the variable name, and the data would be a void* that pointed to whatever you wanted.

    for example:

    int* blah = new int;
    myclassinstance.myvars->put("blah",blah,sizeof(int));

    then later on, you could do:

    void* newblah = myclassinstance.myvars->get("blah");
    int myblah = *((int*)newblah);

    this allows you to access variables by name, and lets you do the type of xml serialization that skinjob mentioned. it also means that serialized data is not dependent on the specific build of your app. that is, later on you can go and modify parts of that myclass, and you will still be able to import your serialized data. w00t.
     

Share This Page