Results 1 to 13 of 13

Thread: Serialization

  1. #1
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default Serialization

    When you serialize an object, does java also serialize all referenced objects from multiple classes? Is there a limit to how many objects you can serialize by serializing one object and all referenced objects therein?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    My understanding of serialization is that it will serialize all object references(assuming they are also serializable), and it will create a "web". I do not believe there is a limit to how much will be serialized either.

    The best way to check this would be to simply make a few small classes and then a final class which has one instance variable for each other class, then in main, instantiate all classes and save the final class(which holds references to the others).

    Create a toString which prints the info of everything in the class, serialize t to disk, deserialize it, then print the object and observe what happens.

    Finally, consider reading this article and see if it helps you out: Discover the secrets of the Java Serialization API

    By googling "serialize java tutorial" or "serialize java information" you should get lots of hits with plenty of details.

  3. #3
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    Thanks. I did actually read one of the links you sent beforehand but it didn't state if there were any limits on number of objects or "hierarchy" of objects.

  4. #4
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    I'm just wondering how I can use serialization while maintaining a MVC structure. I'm thinking of making the source class static as well as a separate serializer class static, with no associations. Then accessing the class through the global variables. Is this a reasonable way to do this?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by garrym View Post
    I'm just wondering how I can use serialization while maintaining a MVC structure. I'm thinking of making the source class static as well as a separate serializer class static, with no associations. Then accessing the class through the global variables. Is this a reasonable way to do this?
    Also, members marked as 'transient' won't be serialized.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    If I made class B implement the serializer class and then tried to serialize a class a A object via a class B object, I would need to cast object A as type class B right?!? and they would need to have the same properties in each class to be castable from one to the other..unless they're declared transient?

  7. #7
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    Sorry, i'm going to rephrase my previous message:

    If I made class B implement the serializer class and then tried to serialize a class A object via a class B static method, I would need to cast object A as type class first right? Would this work if I gave them the same (non transient) properties to both classes?

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by garrym View Post
    Sorry, i'm going to rephrase my previous message:

    If I made class B implement the serializer class and then tried to serialize a class A object via a class B static method, I would need to cast object A as type class first right? Would this work if I gave them the same (non transient) properties to both classes?
    Sorry, I don't understand your question; a class can only be serialized if it implements the (empty) interface Serializable; an object won't be serialized if it is marked as transient. There are no casts involved anywhere.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    If class A is not a subclass (ie extends) class B then you cannot cast from one to the other.

    Class B implementing Serializable will have no effect on either a static method in class B nor on any classes class B happens to use (other than as attributes of a Class B object).

    If you want to be able to serialise class A then you will have to make A implement Serializable.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    The .NET Framework features two serializing technologies:
    And why is .NET at all relevant?

    It isn't...so I can only assume this is a bit of spam to advertise your sig link.

  11. #11
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    Thanks for your comments!

    JosAH, I'm trying to achieve decoupling between the controller and model, hence why I'm trying to use two different classes.

    Tolls, I hoped that would not be a rule about casting objects. Kind of blows my implementation out of the water :\

    Am I right in saying that serialization is not strictly compatible with an MVC framework therefore, as I would have to implement a serializer in the controller class? Is there any way I can implement serialization and still achieve decoupling ?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    What are you trying to do?

    Serialisation requires you to possess the classes that have been serialised, both at the poitn where they're serialised and the point where they're deserialised. So of course source and target are going to be closely coupled.

  13. #13
    garrym is offline Member
    Join Date
    Apr 2011
    Posts
    28
    Rep Power
    0

    Default

    I see, ok thanks anyway. Was just curious more than anyhting.

Similar Threads

  1. Serialization
    By sunde887 in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 11:47 PM
  2. serialization
    By elsa14290 in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2010, 05:03 PM
  3. Serialization
    By thayalan in forum Advanced Java
    Replies: 4
    Last Post: 08-03-2009, 11:22 PM
  4. about serialization
    By bishnu in forum New To Java
    Replies: 0
    Last Post: 12-19-2008, 10:13 AM
  5. Replies: 0
    Last Post: 04-04-2008, 03:47 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
  •