• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialize an object that contains non-serializable objects

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic