This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialize an object that contains non-serializable objects  RSS feed

 
Sub swamy
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i serialize an object - that in turn contains non-serializable objects within it. I have a List object that in turn contains a Map object.

I tried converting the List object to byte[] and re-converted the byte[] to List object. The content of the map is returned as null.

Would appreciate if i could get some help on this.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, of course you can't serialize a non-serializable object. What you can do, though, is make the non-serializable members transient, implement the writeObject() and readObject() methods in your class, and use those methods to save some data that lets you reconstruct the non-serializable object later. The writeObject() method looks like



Where "computeSomethingFrom()" is an imaginary method that returns the information you'd need to reinitialize myTransientField.

Then readObject looks like



where again, "recreateTransientField()" is an imaginary method which reads the data that you wrote in "writeObject()", and returns an instance of the non-serizliable class.

Make sense?
 
Sub swamy
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest for your reply

The problem is face is the following. I have a client code that calls a server code. The server code fetches data and passes it on to the client code.

While the data being fetched on the server side is correct, the client receives the object being passed on to it as null. However, when i try passing a String object from the server to the client - i get the value properly.

The object being passed here is a List that in turn contains a Map (non-serializable object). It is the map object that contains the value. While the server code, populates this Map object correctly and passes it on to the client, at the client side, i find this Map object to be null.

The client here is a JAX-RPC client.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!