How to serialize a class which contains nio.Buffer? - java

I have a class which contains nio buffer as a data member. I am wonderign how to serialize this class?

Make it transient so it is not serialized by default. If you need to serialize the dat in it you can add your own writeObject and readObject methods to do this.

Are you asking how to prevent a specific field from being serialized when implementing Serializable? If so, make the field transient.

Related

Is there any way to serialize a non-serializable class?

I want to save my school objects to my database with serialization. Problem is I can update School class and implement serializable interface but problem is there are too many fields like Student, CityInfo and non of them are serializable. Is there any way to serialize School object with all these fields without updating classes?
public class School{
private Student student;
private CityInfo info;
....
}
You have to implement your own serialization and deserialization methods.
Take a look at the "Customize the protocol" section here.
http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html
As far as Java byte-code serialization is concerned, this cannot be done without customizing the serialization process.
As it says here: http://www.jguru.com/faq/view.jsp?EID=34802
"When you serialize an object, the serialization mechanism works by chaining up the inheritence hierarchy, saving the sate of each Serializable superclass in turn. When serialization reaches the first non-serializable superclass, the serialization stops.
When deserializing, the state of this first non-serializable superclass is restored not from the stream, but by invoking that class' no-argument constructor. If the no-argument constructor is not adequate for your purposes, you must customize the serialization of your subclass with writeObject() and readObject() in order to write out and restore any information from the non-serializable superclass that you find necessary."
However, why must you use the built in Java serialization? There are many libraries out there that will serialize your public fields(and fields with appropriate getters and setters) to JSON, XML or some other format. This also has the added benefit of making your data more portable.

Can a transient variable be serialized in any way?

I faced this question in an interview. Please help me to find the answer. The question was Can a transient variable be serialized in any way?
static and transient fields are not serialized by default.
However they can be serialized if
the same object is accessible via a serialized field.
the object is serialized in a readObject/writeObject or readExternalizable/writeExternalizable.
you are using a different serialization library with different rules (I don't know any which serializes static fields, though I have written such a library by mistake once)
Usually a field is made transient to mean it shouldn't be serialized, though sometimes it might be because
the type is not Serializable
you don't want to use the default Serialization.
Recently in an interview I was also asked this question and here what I answered
Yes, transient member can be serialized. We need to do following to acheive same:
Make your non-serialisable field transient
In writeObject(), first call defaultWriteObject() on the stream to
store all the non-transient fields, then call other methods to
serialize the individual properties of your non-serializable object.
In readObject(), first call defaultReadObject() on the stream to read
back all the non-transient fields, then call other methods
(corresponding to the ones you added to writeObject) to deserialise
your non-serializable object.

Why does not Serializable interface require to implement readObject() and writeObject()

Why is Serializable only marker interface and does not require by contract implementing readObject() and writeObject(). Wouldn't it make more sense?
The serialization mechanism is able to serialize an object without any readObject() and writeObject() methods. But it requires you to specify it objects can or can't be serialized. Some objects hold data, that it makes sense to serialize. Some others don't. For example, serializing a String makes sense, but serializing a Socket or a database Connection doesn't.
So you simply mark which objects may be serialized by making their class implement the Serializable interface.
If you want more control over the way the state of the object is serialized and deserialized, then you can use these two methods.

RMI marshal and serialization

In java using RMI to marshal an object that you are returning from the remote class do you just need to implement Serializable on that object? I have a class node with variables inside that i want to be returned. Do i just implement serializable? If so what about the class that is receiving the object? does its class need to implement serializable too?
example:
public class node implements Serializable{
//variables
//variables
public node(//arguments to constructor here){
}
}
The class that is being serialized needs to implement Serializable. The sending and receiving classes don't. Not sure why you would think otherwise.
If you have a class whose instances you want to serialize using built-in Java serialization, not only must it implement Serializable, all its instance variables must also implement Serializable, or be primitives, or be marked transient (i.e. you tell the JVM that it's okay for them to not get serialized).
If your class can't conform to these constraints for some reason, you can implement custom serialization behavior yourself by implementing Externalizable - then you take responsibility for writing out your object's state and reading it back on the other end.
I'm not sure whether I understand your question correctly or not, but ... if the serializable class has other objects as member variables, then better make them serializable also, otherwise better declare as transient to skip. does this answer your question?
if code inspector program is handy, you can have answer for such question very quickly without posting it
for your tip, only the object you wan to persist or transfer needs to implements Serializable, so the object can be reconstructed as the class structure through serializing/unserializing

How to serialize a non-serializable in Java?

How can I serialize an object that does not implement Serializable? I cannot mark it Serializable because the class is from a 3rd party library.
You can't serialise a class that doesn't implement Serializable, but you can wrap it in a class that does. To do this, you should implement readObject and writeObject on your wrapper class so you can serialise its objects in a custom way.
First, make your non-serialisable field transient.
In writeObject, first call defaultWriteObject on the stream to store all the non-transient fields, then call other methods to serialise the individual properties of your non-serialisable object.
In readObject, first call defaultReadObject on the stream to read back all the non-transient fields, then call other methods (corresponding to the ones you added to writeObject) to deserialise your non-serialisable object.
I hope this makes sense. :-)
Wrap the non-serializable class in a class of your own that implements Serializable. In your class's writeObject method, do whatever's necessary to serialize sufficient information on the non-serializable object so that your class's readObject method can reconstruct it.
Alternatively, contact the developer of the non-serializable class and tell him to fix it. :-)
You can use Kryo. It works on non serialized classes but classes needs registered aforehand.
If your class already implements the Serializable interface (required for serializing), all you must do is to declare the field you don't want to serialize with transient:
public transient String description;
If the class is not final, you can make your own class that extends it and implements Serializable. There are lots of other ways to serialize besides Java's built-in mechanism though.

Categories