Is there is any way in java-world to serialize without need of no-arg constructors and implementation of Serializable?
-
3Java serialization does not require a no argument constructor.Steve Kuo– Steve Kuo2010年11月08日 17:10:06 +00:00Commented Nov 8, 2010 at 17:10
-
2Yes it does. See the Object Serialization Specification and Tom Hawtin's comment.user207421– user2074212010年11月09日 09:19:05 +00:00Commented Nov 9, 2010 at 9:19
-
1EJP, that is not correct, and Steve Kuo is. See docs.oracle.com/javase/7/docs/platform/serialization/spec/… . Your serializable class must "Have access to the no-arg constructor of its first nonserializable superclass". In other words, if you look up the inheritance tree towards Object, the first non-serializable class you see must have a non-private noarg constructor. More to come...Concrete Gannet– Concrete Gannet2012年02月02日 07:47:26 +00:00Commented Feb 2, 2012 at 7:47
-
1Your own serializable class does not require a noarg constructor. See the description of the behaviour of readObject() at docs.oracle.com/javase/7/docs/api/java/io/… . "Reading an object is analogous to running the constructors of a new object. Memory is allocated for the object and initialized to zero (NULL)...". readObject is about to deserialize data into the fields of the object, so it would be wasteful to execute any constructor that would set an incorrect value into those fields. Just zeroing out the memory makes sense.Concrete Gannet– Concrete Gannet2012年02月02日 07:51:46 +00:00Commented Feb 2, 2012 at 7:51
-
@ConcreteGannet As I didn't say a Serializable class requires a no-arg constructor, and as I did say that Serialization requires a no-arg constructor, which your quote also states, I am indeed correct.user207421– user2074212014年03月11日 18:29:21 +00:00Commented Mar 11, 2014 at 18:29
9 Answers 9
Look at XStream, JSX or Google Protocol Buffers.
Comments
JBoss Serialization is a drop-in replacement for standard java serialization, which does not require you to implement java.io.Serializable. Other than that (and the fact that it's much faster), it's the same as the standard serialization mechanism (it even uses the same ObjectInput and ObjectOutput interfaces.
P.S. It has no dependency on other JBoss stuff, it's just one of the JBoss low-level libraries broken out as a separate project.
Comments
A horrific way to do it would be to build a parallel hierarchy of classes, each one standing in for one of the classes in the third-party hierarchy, each of which implements Externalizable, and writes itself by writing the appropriate fields from the third-party object.
I wouldn't, though.
Comments
I believe in some cases you can force serialization despite the Type's declarations. However there is inherent risk as the class may have fields that are not serializable, which will cause runtime exceptions.
I'm curious though as you get no-arg default constructors for free, unless you have written custom constructors. Also implmenting Serializable takes 30 seconds of find/replace.
Is there a reason you are trying to avoid these?
1 Comment
Eishay Smith has done a benchmarking of Java serializers which includes some information about each one, although it doesn't say whether they use no-arg constructors (and in many cases, they don't even work with arbitrary objects, so the question is moot). That might be worth a look.
2 Comments
And Databoard, it can serialize bean-style, record-style and immutable-style classes. You can also write own class-external binding.
Comments
There are a range of alternatives as other people have said. If you want to stick with standard Java serialization with writeObject and readObject, you could write your own adapter class that inherits from a third party class, implement Serializable on your own class, and override writeObject and readObject, in other words, implement custom serialization for your class.
Comments
...and implementation of Serializable?
Unfortunately not. All Serializable objects must implement java.io.Serializable. As for your first part of the question, you can use ObjectInputStream/ObjectOutputStream to serialize objects to byte array and vice versa.
The following example shows how to:
public static byte[] toByteArray(Object object) throws IOException {
if (!isSerializable(object)) {
throw new IOException("Object '" + object.getClass().getName() + "' is not serializable.");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("Closing of ObjectOutputStream failed.", e);
}
}
}
return baos.toByteArray();
}
public static Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
Object object = null;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
object = ois.readObject();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("Closing of ObjectInputStream failed.", e);
}
}
}
return object;
}
2 Comments
Serializable that your class is able to enter serialization.See http://www.jguru.com/faq/view.jsp?EID=251942 explanation.
The only requirement on the constructor for a class that implements Serializable is that the first non-serializable superclass in its inheritence hierarchy must have a no-argument constructor.
Serializeble without no-argument constructor as extends
Objectwith with no-argument constructorpublic class MySerializableClass implements Serializable { public MySerializableClass (...)... }Serializeble without no-argument constructor as extends
MyFirstClasswith with no-argument constructorpublic class MyFirstClass { } public class MySecondClass extends MyFirstClass implements Serializable { public MySecondClass (...)... }NOT serializeble as
MyFirstClassis not implementSerializableAND have not default constructor.public class MyFirstClass { public MyFirstClass (...)... } public class MySecondClass extends MyFirstClass implements Serializable { public MySecondClass (...)... }
Comments
Explore related questions
See similar questions with these tags.