This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Joe Ess
  • salvin francis
  • fred rosenberger

Serialize an object that contains non-serializable objects

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.
author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • 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.
grapes are vegan food pellets. Eat this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!