Results 1 to 16 of 16
Thread: deserialization problem
- 08-05-2011, 08:38 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
deserialization problem
Hi, i would like to transfer serialized classes over to a client host.
i have 1 base class - "BASE"
and 10 derived classes - "DERIVED1" , "DERIVED2", "DERIVED3" ...
is it possible to serialize DERIVED1, send it to client, deserialize it at client side, and finally cast it to BASE?
(this way i save the client side from declaring DERIVED1,DERIVED2,....which are extremely big and complex. this will grant me an "UBER" simplified client which is my goal)
IMPORTANT: I DO need the BASE Fields\methods only on the client side so information loss is irrelevant for the DERIVED classes at the client side.
thanks!
- 08-05-2011, 09:24 PM #2
I think it should work. The default class for the received object is Object with all classes are derived from.
Write a short simple program to test it and let us know what happens.
- 08-06-2011, 03:21 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
ok so, the client recives a DERIVED class named UNKNOWNPROBLEM and tries to deserialize it with the line:
Problem responseProb = (Problem) ois2.readObject();
here, PROBLEM is the BASE class i want to cast into.
again, the client side only defined PROBLEM and doenst know or care about UNKNOWNPROBLEM (and thats why i get the following exceptions:)
Exception in thread "main" java.lang.ClassNotFoundException: Problem.UnknownProblem
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unkno wn Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Client.Client.reciveResponseFromServer(Client.java :100)
at Client.Client.main(Client.java:68)
- 08-06-2011, 04:25 PM #4
The names of classes and packages you show are all different and are confusing and not clear.
The case makes a difference.
Is the package name the same as the class name? Problem
- 08-06-2011, 04:33 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
yes sir. the name of the package is indeed problem as well
- 08-06-2011, 04:36 PM #6
class named UNKNOWNPROBLEM
is not the same as
UnknownProblem
- 08-06-2011, 04:38 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
UNKNOWNPROBLEM stand for UknownProblem.
i tried to make it in caps since its important
the names are identical
- 08-06-2011, 04:40 PM #8
Case is very important when dealing with class names.
Do you have a small test program that compiles and executes that you could post to demonstrate the problem?
- 08-06-2011, 04:42 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
ofcourse.
there is no class named UNKNOWNPROBLEM in my project only UnknownProblem.
keep in mind the client which de serialize doesnt have it and hence the unknown class exception.
what i ask is: do the client must know the class UnknowProblem in order to deserialze?
i rather him knowing only Problem and upcasting all the derived classes he get.
and using only BASE functions
- 08-06-2011, 04:44 PM #10
I have never done it, so I can not say what will happen. Can you write a test program to see.
Then we will both learn something.
- 08-06-2011, 04:47 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
the question standing at hand is:
"when deserializing a DERIVED class, do i must have the exact derived class declared or can i deserialize(and cast) it as BASE?"
my intuition says that serialization need to have the concrete derived class since the case i worte in the question might lead to information loss
- 08-06-2011, 04:50 PM #12
Makes sense. The readObject() method would need a definition for the class it is reading to be able to reconstruct it.
- 08-06-2011, 04:51 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
thanks for your time:)
- 08-06-2011, 05:05 PM #14
Another idea for a test. Make some dummy skeleton class definitions for the derived classes and see what happens.
- 08-06-2011, 05:07 PM #15
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
ill give it a shot. thanks
- 08-08-2011, 10:56 AM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
If you take a peek in the output of a serialised object you'll see near the beginning the (rather mangled in text) classname, which is used by the deserialiser to identify what it is it's supposed to be creating. So you can't use serialisation for this.
Web Services (WSDL) gets around this by simply defining the interface (methods and data required by those methods), and transmitting a standardised lump of xml between client and server. Each end defines its own concrete versions of these interfaces and handles the translation from XML to something "real".
Similar Threads
-
Deserialization problems
By rubikk in forum Advanced JavaReplies: 16Last Post: 07-01-2011, 08:38 PM -
Exception in object deserialization
By hanx in forum Advanced JavaReplies: 3Last Post: 03-01-2011, 07:13 PM -
Singleton serialization / deserialization
By DerekRaimann in forum New To JavaReplies: 4Last Post: 02-28-2011, 01:38 AM -
[SOLVED] Deserialization question
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 04-14-2009, 12:15 AM -
Serialization/Deserialization Error
By andrepezzo in forum Advanced JavaReplies: 2Last Post: 12-16-2008, 05:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks