Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Deserialization

Options
  • 10-05-2007 1:20pm
    #1
    Closed Accounts Posts: 39


    Im just wondering if it's possible to deserialize a java object without having prior information regarding the class? i.e. producer serializes an object, consumer then receives and deserializes it. if that makes sense?


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Google java reflections


  • Closed Accounts Posts: 503 ✭✭✭OMcGovern


    oochie wrote:
    Im just wondering if it's possible to deserialize a java object without having prior information regarding the class? i.e. producer serializes an object, consumer then receives and deserializes it. if that makes sense?

    You can read arbitrary object instances from an ObjectInputStream.
    As long you have the object's class, in your classpath/classloader, it will return an Object to you. Then you can call "instanceof" to work out what it is, or cast it, or use reflection to query fields.

    regards,
    Owen


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    Hi,

    it's just the states ( data ) of the class that is serialized and not the whole class.. All data will be serialized, except for any data defined as transient.

    When you read an object from an ObjectInputStream, it is fairly normal to call the GetClass() method on the object, then once the class is found, cast it to an object of the correct type...

    Object SomeObject = objStreamIn.read();
    if( SomeObject.GetClass().equals("myObject")){
    myObject myobj = (myObject)SomeObject;
    }

    For this to work correctly you will need access to the "myObject" class to convert it back.


Advertisement